###### basic-flags.go ```go package main import ( "flag" "fmt" ) func main() { // // Typed flag parameters: (flag, default value, description) // boolPtr := flag.Bool("bool", false, "bool flag") intPtr := flag.Int("int", 0, "int flag") stringPtr := flag.String("string", "", "string flag") flag.Parse() fmt.Println("boolFlag (--bool) :", *boolPtr) fmt.Println("intFlag (--int) :", *intPtr) fmt.Println("stringFlag (--string) :", *stringPtr) } ``` #### Using (or matching) the default flag options ``` # These achieve the same result: $ go run basic-flags.go $ go run basic-flags.go --bool=false --int=0 --string= $ go run basic-flags.go --bool=0 --int=0 --string="" ``` Output: ``` boolFlag (--bool) : false intFlag (--int) : 0 stringFlag (--string) : ``` #### Explicitly set flag options (overriding default values) ``` # These achieve the same result: $ go run basic-flags.go --bool --int=5 --string=foo $ go run basic-flags.go --bool=true --int=5 --string=foo ``` Output ``` boolFlag (--bool) : true intFlag (--int) : 5 stringFlag (--string) : foo ``` ### A Note about Pointers The astute observer may have noticed the `fmt.Println` statements have asterisks in front of the variables (`boolFlag`, `intFlag`, and `stringFlag`) that were originally declared at the beginning of `main`. This is because they are pointers -- the return values from [flag.Bool()](https://pkg.go.dev/flag#Bool), [flag.Int()](https://pkg.go.dev/flag#Int), and [flag.String()](https://pkg.go.dev/flag#String) are addresses of appropriately typed variables for storing the flag values, not copies of them, because these variables will be updated after `flag.Parse()` is invoked later in the program. Since they're pointers, it's necessary to dereference them when calling a function that expects values. [^1] Understanding that these are pointers to variables allows programmers to take advantage of this by supplying the addresses of variables that already exist, as shown below. This can be convenient, especially when these variables have already been defined for some reason. ```go package main import ( "flag" "fmt" ) func main() { boolValue := false intValue := 0 stringValue := "" // (flag, default value, description) flag.BoolVar(&boolValue, "bool", false, "bool flag") flag.IntVar(&intValue, "int", 0, "int flag") flag.StringVar(&stringValue, "string", "", "string flag") flag.Parse() fmt.Println("boolValue (--bool) :", boolValue) fmt.Println("intValue (--int) :", intValue) fmt.Println("stringValue (--string) :", stringValue) } ``` ``` $ go run basic-flag-variables.go --bool --int=5 --string=foo boolValue (--bool) : true intValue (--int) : 5 stringValue (--string) : foo ``` --- Next: [[8. Piping Data]] [^1]: These are not the same thing as pointers in C and C++. Go pointers are an optimization for working with large blocks of memory that provide performance benefits similar to C and C++, but that's about it. Programmers don't allocate or free memory, can't perform pointer arithmetic, and can't write to memory (Go pointers are references, not memory addresses). See: https://dave.cheney.net/2014/03/17/pointers-in-go.