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
+6.3k Golang : Calculate US Dollar Index (DXY)
+13.7k Golang : Check if an integer is negative or positive
+30.4k Golang : How to verify uploaded file is image or allowed file types
+22.4k Golang : How to read JPG(JPEG), GIF and PNG files ?
+51.1k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+8.4k Golang : Convert word to its plural form example
+7.3k Golang : alternative to os.Exit() function
+6.9k Golang : How to setup a disk space used monitoring service with Telegram bot
+6.5k Unix/Linux : How to get own IP address ?
+10.1k Golang : Compare files modify date example
+4.9k Nginx and PageSpeed build from source CentOS example
+7.2k Golang : Null and nil value