Key links

Tutorials

Set up arguments

Integers

flag.IntVar(POINTER, FLAG_NAME, DEFAULT, DESCRIPTION)

e.g.

package main
 
import (
    "flag"
    "fmt"
)
 
func main() {
    var a, b int
 
    flag.IntVar(&a, "a", 1, "A")
    flag.IntVar(&b, "b", 2, "B")
 
    flag.Parse()
  
    fmt.Println(a * b)
}
$ go run main -a=5

Boolean

Add boolean argument.

flag.Bool(FLAG_NAME), DEFAULT, DESCRIPTION)

e.g.

package main
  
import (
    "flag"
    "fmt"
)
  
func main() {
    foo := flag.Bool("foo", false, "Foo the bar")
  
    flag.Parse()
  
    fmt.Println("Foo:", *foo)
}
$ go run main.go -foo=true
Foo: true