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
| Name | Type | Description |
|---|---|---|
| name | string | Name of the flag or flag set. |
| usage | string | Help text describing the flag. |
| fn | func(string) error | Callback 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)}