Standard library / flag

flag.IntVar()

Registers a flag on the default command line. Binds existing int storage rather than returning a pointer. Signed machine-width integer; accepts decimal, hexadecimal and other supported integer literal forms. Run with arguments: ["-x=7"]. Expected stdout: '7\n'.

Formal Syntax & Structure

go syntaxgo
func IntVar(p *int, name string, value int, usage string)

Parameters & Attributes

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

Working Example

Example Snippetgo
package main
import (  "flag"  "fmt")
func main() { var v int; flag.IntVar(&v, "x", 3, "value"); flag.Parse(); fmt.Println(v) }
Go Reference