Golang flag.StringVar() function example
package flag
StringVar defines a string flag with specified name(2nd parameter), default value(3rd parameter), and usage(4th parameter) string. The argument p (1st parameter) points to a string variable in which to store the value of the flag.
Golang flag.StringVar() function usage example
package main
import (
"flag"
"fmt"
"os"
)
var printHelp bool
var printVerbose string
func setFlags(printHelp *bool) {
flag.BoolVar(printHelp, "help", true, "Print this help message.")
flag.StringVar(&printVerbose, "verbose", "on | off", "Turn verbose mode on or off.") // <-- here
}
func main() {
setFlags(&printHelp)
flag.Parse()
if printHelp {
fmt.Println("-----------------------------")
flag.PrintDefaults()
fmt.Println("-----------------------------")
os.Exit(0)
}
}
Reference :
Advertisement
Something interesting
Tutorials
+20.7k Golang : Saving private and public key to files
+11.7k Golang : How to detect a server/machine network interface capabilities?
+11.6k Android Studio : Create custom icons for your application example
+15.3k Golang : Get all local users and print out their home directory, description and group id
+5.2k Python : Create Whois client or function example
+20k Golang : How to run your code only once with sync.Once object
+10.5k Swift : Convert (cast) String to Integer
+12k Golang : Convert a rune to unicode style string \u
+11.3k Golang : How to use if, eq and print properly in html template
+24.1k Golang : Upload to S3 with official aws-sdk-go package
+17.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+10.3k Golang : Wait and sync.WaitGroup example