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
+19.2k Golang : Populate dropdown with html/template example
+14.5k Golang : Find network of an IP address
+19.9k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+18k Golang : How to log each HTTP request to your web server?
+9.4k Golang : Play .WAV file from command line
+37.7k Golang : Comparing date or timestamp
+6.9k Fix sudo yum hang problem with no output or error messages
+22.1k Golang : Join arrays or slices example
+10.1k Golang : Test a slice of integers for odd and even numbers
+6.1k Golang : Scan forex opportunities by Bollinger bands
+22.6k Generate checksum for a file in Go
+5.9k Golang : Extract unicode string from another unicode string example