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

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