Access raw command line arguments

suggest change

If the flag package or a third-party library doesn’t provide the features you want, you can parse the arguments yourself.

func main() {
	fmt.Printf("Name of executable: '%s'\n", os.Args[0])
	args := os.Args[1:]
	for i, arg := range args {
		fmt.Printf("Arg %d, value: '%s'\n", i, arg)
	}
}
Name of executable: '/tmp/go-build250483369/b001/exe/main'
Arg 0, value: '-echo'
Arg 1, value: 'echo-arg'
Arg 2, value: 'additional'
Arg 3, value: 'arg'

The above output is a result of go run $file -echo echo-arg additional arg.

Raw arguments

Raw command-line arguments are the []string slice os.Args.

The first element is the name of the executable.

The remaining elements are the command-line arguments.

On Windows the command line is a single UTF-16 string. The Go runtime converts it to UTF-8 and splits it into arguments so that programs handle arguments the same way on every operating system.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents