Created
December 28, 2019 19:54
-
-
Save tomcam/12e760b51e65d541e0fafc413fe90bbc to your computer and use it in GitHub Desktop.
Golang flag bool examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* Golang command-line flag tutorial example for boolean flag | |
Go Playground example at: | |
https://play.golang.org/p/fEaMQCjxQUT | |
Assuming the program is named ./t here's example usage and output: | |
$ ./t | |
Verbose mode set? false | |
$ ./t -v | |
Verbose mode set? true | |
$ ./t -v=t | |
Verbose mode set? true | |
$ ./t -v=f | |
Verbose mode set? false | |
$ ./t -v=true | |
Verbose mode set? true | |
$ ./t -v=false | |
Verbose mode set? false | |
$ ./t -v=fa | |
invalid boolean value "fa" for -v: parse error | |
Usage of ./t: | |
-v Explain what's happening while program runs | |
*/ | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
) | |
func main() { | |
pVerbose := flag.Bool("v", false, "Explain what's happening while program runs") | |
flag.Parse() | |
fmt.Fprintf(os.Stdout, "Verbose mode set? %v\n", *pVerbose) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment