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
+37.5k Upload multiple files with Go
+32.1k Golang : Validate email address with regular expression
+16.9k Golang : How to generate QR codes?
+19.2k Golang : Check if directory exist and create if does not exist
+7.4k Golang : Individual and total number of words counter example
+5.3k Python : Convert(cast) string to bytes example
+9.2k Golang : How to control fmt or log print format?
+7.5k Golang : Detect sample rate, channels or latency with PortAudio
+43.3k Golang : Convert []byte to image
+25k Golang : Create PDF file from HTML file
+5.7k Golang : Error handling methods