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
+10.3k Golang : Convert file unix timestamp to UTC time example
+22.6k Generate checksum for a file in Go
+33.6k Golang : How to check if slice or array is empty?
+17k Golang : How to save log messages to file?
+6k Golang : Function as an argument type example
+6.7k Golang : Reverse by word
+14.5k How to automatically restart your crashed Golang server
+17.8k Golang : Defer function inside init()
+9.7k Random number generation with crypto/rand in Go
+17.7k How to enable MariaDB/MySQL logs ?
+13.2k Golang : Convert(cast) int to int64
+18.5k Golang : Send email with attachment