Standard library / flag
flag.FlagSet.Func()
Invokes a callback once per flag occurrence; callback errors become parse errors. Run with arguments: ["-tag=a", "-tag=b"]. Expected stdout: '[a b]\n'.
Formal Syntax & Structure
go syntaxgo
func (f *FlagSet) Func(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" "os")
func check(e error) { if e != nil { panic(e) }}
func main() { f := flag.NewFlagSet("demo", flag.ContinueOnError) var seen []string f.Func("tag", "tag", func(s string) error { seen = append(seen, s); return nil }) check(f.Parse(os.Args[1:])) fmt.Println(seen)}