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
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+35.5k Golang : Smarter Error Handling with strings.Contains()
+5.2k Golang : Customize scanner.Scanner to treat dash as part of identifier
+9.4k Golang : How to protect your source code from client, hosting company or hacker?
+18.6k Golang : Generate thumbnails from images
+8k Findstr command the Grep equivalent for Windows
+13.6k Golang : Strings comparison
+16.4k Golang : Convert slice to array
+23.1k Golang : Randomly pick an item from a slice/array example
+5.2k Golang : Calculate half life decay example
+6.8k Golang : Muxing with Martini example
+20.9k Golang : Convert PNG transparent background image to JPG or JPEG image