Command-line arguments
are a common way to parameterize execution of programs.
For example, |
|
package main
|
|
import "os"
import "fmt"
|
|
func main() {
|
|
|
argsWithProg := os.Args
argsWithoutProg := os.Args[1:]
|
You can get individual args with normal indexing. |
arg := os.Args[3]
|
fmt.Println(argsWithProg)
fmt.Println(argsWithoutProg)
fmt.Println(arg)
}
|
To experiment with command-line arguments it’s best to
build a binary with |
$ go build command-line-arguments.go
$ ./command-line-arguments a b c d
[./command-line-arguments a b c d]
[a b c d]
c
|
Next we’ll look at more advanced command-line processing with flags. |
Previous example: Line Filters.
Next example: Command-Line Flags.