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.1k Golang : How To Use Panic and Recover
+21.1k Golang : For loop continue,break and range
+7k Golang : constant 20013 overflows byte error message
+17k Golang : Covert map/slice/array to JSON or XML format
+11.6k Golang : Convert(cast) float to int
+8.4k Golang : Convert word to its plural form example
+19.1k Golang : Clearing slice
+21.2k Golang : Clean up null characters from input data
+21.2k Golang : How to get time zone and load different time zone?
+19.2k Golang : Check if directory exist and create if does not exist
+14.8k Golang : Find commonalities in two slices or arrays example
+9.8k Golang : Format strings to SEO friendly URL example