Golang flag.PrintDefaults() function example
package flag
PrintDefaults prints to standard error the default values of all defined command-line flags.
Golang flag.PrintDefaults() 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.")
}
func main() {
setFlags(&printHelp)
flag.Parse()
if printHelp {
fmt.Println("-----------------------------")
flag.PrintDefaults()
fmt.Println("-----------------------------")
os.Exit(0)
}
}
Output :
-----------------------------
-help=true: Print this help message.
-verbose="on | off": Turn verbose mode on or off.
-----------------------------
Reference :
Advertisement
Something interesting
Tutorials
+20.5k nginx: [emerg] unknown directive "passenger_enabled"
+12.2k Golang : Get remaining text such as id or filename after last segment in URL path
+15.8k Golang : Get digits from integer before and after given position example
+9.7k Random number generation with crypto/rand in Go
+18.2k Golang : Get command line arguments
+37.5k Golang : Converting a negative number to positive number
+23.5k Golang : Read a file into an array or slice example
+14.6k Golang : Reset buffer example
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+25.7k Golang : How to write CSV data to file
+13k Golang : Get terminal width and height example
+22k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."