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
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*