Standard library / flag

flag.FlagSet.Set()

Sets a registered flag from text, invoking its parser; returns an error for unknown or invalid values. Expected stdout: '9\n'.

Formal Syntax & Structure

go syntaxgo
func (f *FlagSet) Set(name, value string) error

Parameters & Attributes

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

Return Value

error

Parsing or setting error; nil on success.

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.Int("x", 0, "value")  _ = v  check(f.Parse(os.Args[1:]))  check(f.Set("x", "9"))  fmt.Println(*v)}
Go Reference