doushou1298 2016-05-27 15:24
浏览 419
已采纳

使用filepath.Glob的Golang怪异行为

I'm confused about glob usage on Golang, I'm probably missing some environment variable. I don't know if I'm doing it right.

This code works perfectly fine when ran on my IDE (Intellij IDEA), but when this is ran on the OS via go run it doesn't work. I can't figure out what is the difference.

package main

import (
    "path/filepath"
    "fmt"
    "os"
)

func main() {

    file := os.Args[1]

    matches, err := filepath.Glob(file)

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(fmt.Sprintf("Number of matches:%d", len(matches)))
    fmt.Println(matches)
}

Running on OS

go run globtest.go /Users/bernardovale/glotest/*.bkp
Number of matches:1
[/Users/bernardovale/glotest/test1.bkp]

ls -l /Users/bernardovale/glotest/*.bkp
-rw-r--r--  1 bernardovale  staff  0 May 27 12:06 /Users/bernardovale/glotest/test1.bkp
-rw-r--r--  1 bernardovale  staff  0 May 27 12:06 /Users/bernardovale/glotest/test2.bkp
-rw-r--r--  1 bernardovale  staff  0 May 27 12:06 /Users/bernardovale/glotest/test3.bkp

Running on IntelliJ IDEA

go run on the IDEA

  • 写回答

1条回答 默认 最新

  • dpmkif3097 2016-05-27 16:43
    关注

    The difference here is that the shell is executing the glob and providing the individual values to your application. When executing from a shell you should wrap a glob in double quotes to make sure it's not evaluated by your shell first. See the example below.

    Seans-MBP-2:~ sthorne$ echo Testing*
    Testing Testing2 Testing3
    Seans-MBP-2:~ sthorne$ echo "Testing*"
    Testing*
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器