notes

Personal notes
git clone git://git.laack.co/notes.git
Log | Files | Refs

Flag.md (829B)


      1 # flag
      2 
      3 **Source:** [https://pkg.go.dev/flag](https://pkg.go.dev/flag)
      4 
      5 ## Usage
      6 
      7 ### Example 1
      8 
      9 ```go
     10 
     11 func main {
     12     version := flag.Bool("version", false, "Show version number")
     13     flag.Parse()
     14 
     15     if version {
     16         fmt.Println("v1.0.0")
     17         return
     18     }
     19     // ...
     20 }
     21 
     22 ```
     23 
     24 The above code defines a flag `version` with a default value of false and the help menu text of "Show version number". When the program is executed with the `--version` flag it will print the hardcoded version number and return. If the user passes the `--help` flag it will show the help menu which explains the usage of the version flag.
     25 
     26 NOTE: The --help flag is a built-in flag (can be overrided). Once the program reaches the line with `flag.Parse` it will print all of the defined flags along with their help text and then return early.