Skip to content

The `flag` package in Go

The flag package in Go

The flag package in Go is used to parse command-line flags. Here are some of the most commonly used functions from the flag package:

  1. flag.String()
    Defines a string flag with a default value and a description.

    var name = flag.String("name", "default", "your name")
  2. flag.Int()
    Defines an integer flag with a default value and a description.

    var age = flag.Int("age", 30, "your age")
  3. flag.Bool()
    Defines a boolean flag with a default value and a description.

    var verbose = flag.Bool("verbose", false, "enable verbose output")
  4. flag.Parse()
    Parses the command-line flags. This must be called after all flags are defined.

    flag.Parse()
  5. flag.Args()
    Returns the non-flag arguments after parsing.

    args := flag.Args()
  6. flag.NArg()
    Returns the number of non-flag arguments.

    numArgs := flag.NArg()
  7. flag.NFlag()
    Returns the number of flags that have been set.

    numFlags := flag.NFlag()
  8. flag.Float64()
    Defines a float64 flag with a default value and description.

    var weight = flag.Float64("weight", 75.5, "your weight in kg")
  9. flag.Duration()
    Defines a duration flag (time.Duration) with a default value and description.

    var timeout = flag.Duration("timeout", 10*time.Second, "timeout duration")
  10. flag.Set()
    Allows setting a flag programmatically.

    flag.Set("name", "John")
  11. flag.Lookup()
    Returns the flag with the given name (if it exists).

    f := flag.Lookup("name")

These functions allow you to define and manage flags in your Go programs, making it easy to handle user input from the command line.