Golang flag.BoolVar() function example

package flag

BoolVar defines a bool flag with specified name(2nd parameter), default value(3rd parameter), and usage string(4th parameter). The argument p(1st parameter) points to a bool variable in which to store the value of the flag.

Golang flag.BoolVar() function usage example

 package main

 import (
 "flag"
 "fmt"
 )

 var usejson bool

 func init() {
 flag.BoolVar(&usejson, "usejson", false, "When true, produce result in JSON blocks. Default : 'false'")
 }

 func main() {
 fmt.Println(usejson) // print boolean value

 fmt.Println(flag.Lookup("usejson")) // print Flag struct
 }

Output :

false

&{usejson When true, produce result in JSON blocks. Default : 'false' false false}

Reference :

http://golang.org/pkg/flag/#BoolVar

Advertisement