Home
Go
flag Examples
Updated Jul 6, 2023
Dot Net Perls
Flag. Flags are arguments passed to a program. In Go, we parse these flags with the flag package. We call funcs like flag.Int and flag.String.
Package notes. With the flag package, we must first register flags. Then we call flag.Parse() to parse these in from the command line.
Flag.Int example. Here we register an Int flag by calling the flag.Int function in main(). Note that the "flag" package is imported in the import block at the top.
Then The flag.Int func returns an Int pointer. We access its Int value with the star character.
Info We can copy the value from the Int pointer into an Int with an assignment statement. We do this with the "value" local.
Here We invoke the Go executable and pass the "run" argument to it. We pass the program file name and specify the count argument.
Result The second argument is the default value for that flag. If Parse() is called and no value is found, 5 will be used.
package main import ( "flag" "fmt" ) func main() { // Register Int flag. count := flag.Int("count", 5, "count of iterations") // Parse the flags. flag.Parse() // Print the argument. fmt.Println("Argument", *count) // Get int from the Int pointer. value := *count fmt.Println("Value", value) }
C:\Go\bin\Go.exe run C:\programs\file.go -count=20
Argument 20 Value 20
Flag.String. Let us consider a String argument. With a String, no special parsing method is invoked internally, but we must still call Parse.
Here We specify a special "greeting" string, and then uppercase it as the program executes.
package main import ( "flag" "fmt" "strings" ) func main() { // A string flag. greeting := flag.String("greeting", "Hello", "startup message") flag.Parse() // Get String. // ... Uppercase it for emphasis. value := *greeting fmt.Println("Program says:", strings.ToUpper(value)) }
C:\Go\bin\Go.exe run C:\programs\file.go -greeting=Hey
Program says: HEY
Command line syntax. We can omit the equals sign when using an argument on the command line. But a leading hyphen is required. And case is important.
C:\Go\bin\Go.exe run C:\programs\file.go -greeting Hey Program says: HEY C:\Go\bin\Go.exe run C:\programs\file.go greeting Hey Program says: HELLO C:\Go\bin\Go.exe run C:\programs\file.go GREETING=Hey Program says: HELLO
Invalid argument. With a func like flag.Int we must pass an argument that can be parsed correctly. We cannot parse the string "bird" as an Int. An error occurs.
ParseInt
package main import ( "flag" "fmt" ) func main() { // A string flag. size := flag.Int("size", 0, "file size") flag.Parse() // Print size. fmt.Println(*size) }
C:\Go\bin\Go.exe run C:\programs\file.go -size=bird
invalid value "bird" for flag -size: strconv.ParseInt: parsing "bird": invalid syntax Usage of C:\...\go-build032374134\command-line-arguments\_obj\exe\file.exe: -size int file size exit status 2
A summary. With command lines, programs communicate with other programs. Simple arguments can be passed directly. With "flag," a Go package, we make this communication easier.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jul 6, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen