Golang flag.FlagSet.StringVar() function example
package flag
StringVar defines a string flag with specified name(2nd parameter), default value(3rd parameter), and usage string(4th parameter). The argument p(1st parameter) points to a string variable in which to store the value of the flag.
Golang flag.FlagSet.StringVar() function usage example
package main
import (
"flag"
"fmt"
)
var (
flagSet *flag.FlagSet
p string
)
func main() {
flagSet = flag.NewFlagSet("example", flag.ContinueOnError)
filename := flagSet.String("file", "textfile.txt", "filename to view")
flagSet.StringVar(&p, "timeout", "5000", "time to wait for response") // <-- here
err := flagSet.Parse([]string{"file", "timeout"})
if err != nil {
fmt.Println(err)
}
fmt.Println(*filename)
fmt.Println(p)
}
Output :
textfile.txt
5000
Reference :
Advertisement
Something interesting
Tutorials
+8.3k Golang : Configure Apache and NGINX to access your Go service example
+8.6k Golang : Convert(cast) []byte to io.Reader type
+10.6k Golang : Bubble sort example
+5k Golang : Constant and variable names in native language
+10.6k Golang : How to delete element(data) from map ?
+12.3k Golang : List running EC2 instances and descriptions
+11.4k Golang : Concatenate (combine) buffer data example
+5.1k Golang : Display packages names during compilation
+9.1k Golang : Handle sub domain with Gin
+34k Golang : Proper way to set function argument default value
+8.8k Golang : Gorilla web tool kit schema example
+17k Golang : Get input from keyboard