- Args
- os.Args
Args - (argv) command line arguments
The first element of this slice (index 0) is the path to the executable. If we run this using go run then it will be some strange-looking temporary location.
len gives us the number of elements on the command line that will be 1 if there are no parameters on the command line.
Using a square-bracket after the name os.Args allows us to access the specific elements in the slice.
Later we'll learn higher level abstractions to read command line parameters, but for now this will be enough.
examples/cli/cli.go
package main import ( "fmt" "os" ) func main() { fmt.Println(len(os.Args)) fmt.Println(os.Args) fmt.Println(os.Args[0]) // the path to the compiled executable fmt.Printf("%T\n", os.Args) // []string (slice) }
go run examples/cli/cli.go hello world
3 [/tmp/go-build781021115/b001/exe/cli hello world] /tmp/go-build781021115/b001/exe/cli []string