Standard library / flag

flag.String()

Registers a flag on the default command line. 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 String(name string, value string, usage string) *string

Parameters & Attributes

NameTypeDescription
namestringName of the flag or flag set.
valuestringDefault value for registration, or text to parse for Set.
usagestringHelp text describing the flag.

Return Value

*string

Registers a flag on the default command line.

Working Example

Example Snippetgo
package main
import (  "flag"  "fmt")
func main() { v := flag.String("x", "default", "value"); flag.Parse(); fmt.Println(*v) }
Go Reference