Standard library / flag

flag.FlagSet.StringVar()

Registers a flag on an independent FlagSet. 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 (f *FlagSet) StringVar(p *string, name string, value string, usage string)

Parameters & Attributes

NameTypeDescription
p*stringPointer to storage updated during parsing.
namestringName of the flag or flag set.
valuestringDefault value for registration, or text to parse for Set.
usagestringHelp text describing the flag.

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 v string  f.StringVar(&v, "x", "default", "value")  check(f.Parse(os.Args[1:]))  fmt.Println(v)}
Go Reference