Go command-line arguments
last modified April 11, 2024
In this article we show how to pass command-line arguments into Go programs.
Command-line arguments
Command-line arguments are options and data that are passed to programs. We usually pass arguments to console programs, but sometimes we pass arguments to GUI programs as well.
The os.Args
holds the command-line arguments. The first value in
this slice is the name of the program, while the os.Args[1:]
holds the arguments to the program. The individual arguments are accessed with
indexing operation.
$ go version go version go1.22.2 linux/amd64
We use Go version 1.22.2.
package main import ( "fmt" "os" "reflect" ) func main() { prg_name := os.Args[0] fmt.Printf("The program name is %s\n", prg_name) names := os.Args[1:] fmt.Println(reflect.TypeOf(names)) for _, name := range names { fmt.Printf("Hello, %s!\n", name) } }
The example receives command-line arguments.
prg_name := os.Args[0] fmt.Printf("The program name is %s\n", prg_name)
We get and print the first argument, which is the program name.
names := os.Args[1:]
We get all the received arguments.
fmt.Println(reflect.TypeOf(names))
We print the type which holds the arguments (slice).
for _, name := range names { fmt.Printf("Hello, %s!\n", name) }
We go through the arguments and build a message from each of them.
$ go build read_args.go $ ./read_args Jan Peter Lucia The program name is ./read_args []string Hello, Jan! Hello, Peter! Hello, Lucia!
We build the program and run it. We pass the program three names on the command line.
Source
In this article we have covered passing command-line arguments to Go programs.
Author
List all Go tutorials.