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 Useful methods to access blocked websites
+8.3k Golang : Emulate NumPy way of creating matrix example
+6.9k Default cipher that OpenSSL used to encrypt a PEM file
+39k Golang : How to iterate over a []string(array)
+8.1k Golang : How To Use Panic and Recover
+13.8k Generate salted password with OpenSSL example
+17.5k Golang : Clone with pointer and modify value
+7.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+5.3k PHP : Hide PHP version information from curl
+10.4k Golang : Generate random integer or float number
+8.6k Golang : Another camera capture GUI application with GTK and OpenCV
+9k Golang : Inject/embed Javascript before sending out to browser example