Standard library / flag
flag.FlagSet.TextVar()
Binds a type implementing encoding.TextUnmarshaler, with a compatible default implementing TextMarshaler. Run with arguments: ["-ip=192.0.2.7"]. Expected stdout: '192.0.2.7\n'.
Formal Syntax & Structure
go syntaxgo
func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| p | encoding.TextUnmarshaler | Pointer to storage updated during parsing. |
| name | string | Name of the flag or flag set. |
| value | encoding.TextMarshaler | Default value for registration, or text to parse for Set. |
| usage | string | Help text describing the flag. |
Working Example
Example Snippetgo
package main
import ( "flag" "fmt" "net/netip" "os")
func check(e error) { if e != nil { panic(e) }}
func main() { f := flag.NewFlagSet("demo", flag.ContinueOnError) var ip netip.Addr f.TextVar(&ip, "ip", netip.MustParseAddr("192.0.2.1"), "address") check(f.Parse(os.Args[1:])) fmt.Println(ip)}