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

/var/folders/ry/xrnmhhjx5_sdyq4mv8dk54s00000gn/T/go-build749979406/b001/exe/main

One thought on “GoLang: Command line arguments

Leave a Reply

Your email address will not be published.