Standard library / flag

flag.FlagSet.BoolFunc()

Invokes a callback for each boolean-style flag occurrence, including an implicit true. Run with arguments: ["-verbose", "-verbose=false"]. Expected stdout: '[true false]\n'.

Formal Syntax & Structure

go syntaxgo
func (f *FlagSet) BoolFunc(name, usage string, fn func(string) error)

Parameters & Attributes

NameTypeDescription
namestringName of the flag or flag set.
usagestringHelp text describing the flag.
fnfunc(string) errorCallback invoked during parsing or visitation.

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 seen []string  f.BoolFunc("verbose", "verbose", func(s string) error { seen = append(seen, s); return nil })  check(f.Parse(os.Args[1:]))  fmt.Println(seen)}
Go Reference