Standard library / flag
flag.StringVar()
Registers a flag on the default command line. Binds existing string storage rather than returning a pointer. String value; the shell must preserve spaces as part of one argument. Run with arguments: ["-x=hello world"]. Expected stdout: 'hello world\n'.
Formal Syntax & Structure
go syntaxgo
func StringVar(p *string, name string, value string, usage string)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| p | *string | Pointer to storage updated during parsing. |
| name | string | Name of the flag or flag set. |
| value | string | 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")
func main() { var v string; flag.StringVar(&v, "x", "default", "value"); flag.Parse(); fmt.Println(v) }