Standard library / flag

flag.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 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")
func main() {  var seen []string  flag.BoolFunc("verbose", "verbose", func(s string) error { seen = append(seen, s); return nil })  flag.Parse()  fmt.Println(seen)}
Go Reference