Standard library / flag
flag.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 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")
func main() { var seen []string flag.Func("tag", "tag", func(s string) error { seen = append(seen, s); return nil }) flag.Parse() fmt.Println(seen)}