Intellij idea 使用filepath.Glob的Golang异常行为

Intellij idea 使用filepath.Glob的Golang异常行为,intellij-idea,go,glob,Intellij Idea,Go,Glob,我对Golang上的glob用法感到困惑,我可能缺少一些环境变量。我不知道我做得对不对 这段代码在我的IDE(Intellij IDEA)上运行时效果非常好,但在操作系统上通过go run运行时就不起作用了。我想不出有什么区别 package main import ( "path/filepath" "fmt" "os" ) func main() { file := os.Args[1] matches, err := filepath.Glo

我对Golang上的glob用法感到困惑,我可能缺少一些环境变量。我不知道我做得对不对

这段代码在我的IDE(Intellij IDEA)上运行时效果非常好,但在操作系统上通过
go run
运行时就不起作用了。我想不出有什么区别

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)
}
在操作系统上运行

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
在IntelliJ IDEA上运行


这里的区别在于shell正在执行glob并向应用程序提供各个值。当从shell执行时,应该将glob用双引号括起来,以确保它不是由shell首先计算的。请参见下面的示例

Seans-MBP-2:~ sthorne$ echo Testing*
Testing Testing2 Testing3
Seans-MBP-2:~ sthorne$ echo "Testing*"
Testing*

你让贝壳为你做地球仪。从你的程序中打印出args,看看你得到了什么。你是对的,我不知道。谢谢。如果你愿意,把它作为答案贴出来。