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

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.Func("tag", "tag", func(s string) error { seen = append(seen, s); return nil })  flag.Parse()  fmt.Println(seen)}
Go Reference