flag.Bool() Function in Golang With Examples

Go language provides inbuilt support for command-line parsing and has functions that could be used to define flags to be used with a command-line program using the flag package. This package provides the flag.Bool() function which is used to define a boolean flag with the specified name, default value, and usage string.

Syntax:

func Bool(name string, value bool, usage string) *bool

Parameters: This function accepts three parameters as mentioned above and described below:

  • name: It is a string that specifies the name to be used for the flag.
  • value: It is a boolean value that specifies the default value to be used by the flag.
  • usage: It is a string that specifies the usage or help message to be shown for the flag.

Return Value: It returns an address of the boolean variable that stores the value of the flag defined.

Below programs illustrate the flag.Bool() function:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate
// the flag.Bool() Function
package main
  
import (
    "flag"
    "fmt"
)
  
func main() {
    // Define a bool flag
    boolArgPtr := flag.Bool("arg1", false, "This is a bool argument")
  
    // Parse command line 
    // into the defined flags
    flag.Parse()
  
    fmt.Println("Bool Arg:", *boolArgPtr)
}

chevron_right


Output:

  • Specifying the flag value
    $ go run ex1.go -arg1=true
    Bool Arg: true
  • Not specifying the flag value (Default Value)
    $ go run ex1.go
    Bool Arg: false
    

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate
// the flag.Bool() Function
package main
  
import (
    "flag"
    "fmt"
)
  
func main() {
  
    // Define multiple bool arguments
    plainArgPtr := flag.Bool("plaintext", false, "Enable plaintext")
    jsonArgPtr := flag.Bool("json", false, "Enable JSON")
    csvArgPtr := flag.Bool("csv", false, "Enable CSV")
  
    // Parse command line into the defined flags
    flag.Parse()
  
    fmt.Println("Enable plaintext:", *plainArgPtr)
    fmt.Println("Enable JSON:", *jsonArgPtr)
    fmt.Println("Enable CSV:", *csvArgPtr)
}

chevron_right


Output

  • Specifying some flag values
    $ go run ex2.go -plaintext=true -csv=true
    Enable plaintext: true
    Enable JSON: false
    Enable CSV: true
    
  • Not specifying any flag value (Default Values)
    $ go run ex2.go
    Enable plaintext: false
    Enable JSON: false
    Enable CSV: false
    



My Personal Notes arrow_drop_up

Im listening

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.