Standard library / flag
flag.FlagSet.String()
Registers a flag on an independent FlagSet. 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) String(name string, value string, usage string) *stringParameters & Attributes
| Name | Type | Description |
|---|---|---|
| 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. |
Return Value
*string
Registers a flag on an independent FlagSet.
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) v := f.String("x", "default", "value") check(f.Parse(os.Args[1:])) fmt.Println(*v)}