Standard library / flag
flag.FlagSet.BoolVar()
Registers a flag on an independent FlagSet. Binds existing bool storage rather than returning a pointer. Boolean value; a bare -x means true, and -x=false explicitly disables it. Run with arguments: ["-x=true"]. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| p | *bool | Pointer to storage updated during parsing. |
| name | string | Name of the flag or flag set. |
| value | bool | Default value for registration, or text to parse for Set. |
| usage | string | Help text describing the flag. |
Working Example
Example Snippetgo
package main
import ( "flag" "fmt" "os")
func check(e error) { if e != nil { panic(e) }}
func main() { f := flag.NewFlagSet("demo", flag.ContinueOnError) var v bool f.BoolVar(&v, "x", false, "value") check(f.Parse(os.Args[1:])) fmt.Println(v)}