Golang flag.IntVar() function example

package flag

IntVar defines an int flag with specified name (2nd parameter) , default value (3rd parameter), and usage string (4th parameter). The argument p (1st parameter) points to an int variable in which to store the value of the flag.

Golang flag.IntVar() function usage example

 package main

 import (
 "flag"
 "fmt"
 )

 var portnum int

 func main() {

 flag.IntVar(&portnum, "port", 8080, "The port number to serve HTTP.")

 fmt.Println(flag.Lookup("port")) // print Flag struct

 fmt.Println("port number : ", portnum)
 }

Output :

&{port The port number to serve HTTP. 8080 8080}

port number : 8080

Reference :

http://golang.org/pkg/flag/#IntVar

Advertisement