Standard library / flag

flag.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 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")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  v := flag.Int("x", 0, "value")  _ = v  flag.Parse()  check(flag.Set("x", "9"))  fmt.Println(*v)}
Go Reference