↓ Skip to main content
Go

GoLang: Command line arguments

·1 min

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
Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Files: GoLang

·2 mins
In the last blog, I tried reading a file in GoLang. My repository in GitHub has all the code that I write (link) Now, I will extend the same program to write to the same file.

Files: GoLang

·4 mins
Learning a new syntax and the libraries is always a task, and the only way forward is gradual. In this blog I will try to create a small program to read a text file and will keep updating the program to include more sophistication

GOLang: Reflection

·3 mins
Must Read # https://blog.golang.org/laws-of-reflection https://golang.org/pkg/reflect/ https://golang.org/pkg/reflect/#Kind https://golang.org/pkg/reflect/#Value Package reflect implements run-time reflection, allowing a program to manipulate objects with arbitrary types. Reflection is a way to examine own structure. Let’s try and learn it via examples.