GoLang: Command line arguments

Saurabh Sharma

Reading the golang official documentation is fun, today I saw the Variables in package os

var Args []string

It allows you to hold command line arguments and a simple main program that can help you validate this.

func main() {
	if len(os.Args) > 0 {
		fmt.Println(" More than one argument found: " + fmt.Sprintf("%s", os.Args[1:]))
	} else {
		fmt.Println(" No arguments passed ")

go run main 1 2 3 4

More than one argument found: [1 2 3 4]

Why I used os.Args[1:]

The argument 0 is the location of the exe itself in my case


One thought on “GoLang: Command line arguments

Comments are closed.