Command-line arguments are a way to supply additional information to a program when it is started. The easiest way to supply command line arguments is by specifying a space separated list of values after the command while running it:

$ ./my-program Arg1 Arg2 Arg3

Read Command line arguments in Go

In Go, you can access the raw command-line arguments using the os.Args variable. It is a slice and it holds all the command-line arguments starting with the program name.

package main

import (
	"fmt"
	"os"
)

func main() {
	args := os.Args
	fmt.Printf("All arguments: %v\n", args)

	argsWithoutProgram := os.Args[1:]
	fmt.Printf("Arguments without program name: %v\n", argsWithoutProgram)
}
$ go build command-line-arguments.go

$ ./command-line-arguments Hello World From Command Line
All arguments: [./command-line-arguments Hello World From Command Line]
Arguments without program path: [Hello World From Command Line]

Go Command Line Arguments example

Here is another example that reads a bunch of names from the command line and says Hello to all the names:

package main

import (
	"fmt"
	"os"
)

func main() {
	names := os.Args[1:]

	for _, name := range names {
		fmt.Printf("Hello, %s!\n", name)
	}
}
$ go build say-hello-to.go
$ ./say-hello-to Rajeev Sachin Jack Daniel
Hello, Rajeev!
Hello, Sachin!
Hello, Daniel!