Standard library / flag
flag.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 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")
func main() { var ip netip.Addr flag.TextVar(&ip, "ip", netip.MustParseAddr("192.0.2.1"), "address") flag.Parse() fmt.Println(ip)}