Flag.Bool() Function in Golang With Examples


In Golang, the flag package provides a way to parse command-line arguments. It allows us to define flags that can be set when running a program from the command line. The flag.Bool() function is used to define a boolean flag. It creates a new bool flag with the specified name, default value, and usage string. In this article, we will explore the flag.Bool() function with examples.

Syntax

The syntax of the flag.Bool() function is as follows −

flag.Bool(name string, default bool, usage string) *bool

Parameters

  • name − A string that specifies the name of the flag.

  • default − A boolean value that specifies the default value of the flag.

  • usage − A string that specifies the usage message of the flag.

Return Value

The flag.Bool() function returns a pointer to a bool variable that stores the value of the flag.

Example 1: Simple Usage

In this example, we will create a boolean flag named is_admin with a default value of false.

package main

import (
   "flag"
   "fmt"
)

func main() {
   // Define a boolean flag named "is_admin" with a default value of false
   isAdmin := flag.Bool("is_admin", false, "specifies whether the user is an admin or not")
   
   // Parse the command-line arguments
   flag.Parse()
   
   // Print the value of the flag
   fmt.Println("Is Admin:", *isAdmin)
}

Output

Is Admin: false

In the above example, we defined a boolean flag named is_admin with a default value of false. We then parsed the command-line arguments using the flag.Parse() function and printed the value of the flag using *isAdmin.

Example 2: Using the Flag in If Statement

In this example, we will check the value of the flag is_admin and print a message accordingly.

package main

import (
   "flag"
   "fmt"
)

func main() {
   // Define a boolean flag named "is_admin" with a default value of false
   isAdmin := flag.Bool("is_admin", false, "specifies whether the user is an admin or not")
   
   // Parse the command-line arguments
   flag.Parse()
   
   // Check if the user is an admin
   if *isAdmin {
      fmt.Println("User is an Admin")
   } else {
      fmt.Println("User is not an Admin")
   }
}

Output

User is not an Admin

Example 3: Using the Flag in a Function

In this example, we will create a function that takes a boolean flag as an argument.

package main

import (
   "flag"
   "fmt"
)

func myFunction(isAdmin *bool) {
   if *isAdmin {
      fmt.Println("User is an Admin")
   } else {
      fmt.Println("User is not an Admin")
   }
}
   
func main() {
   // Define a boolean flag named "is_admin" with a default value of false
   isAdmin := flag.Bool("is_admin", false, "specifies whether the user is an admin or not")
   
   // Parse the command-line arguments
   flag.Parse()
   
   // Call the function with the "is_admin" flag
   myFunction(isAdmin)
}

Output

User is not an Admin

Conclusion

flag.Bool() function in Golang is a useful tool for working with command-line arguments. It allows for easy parsing and handling of boolean flags, which are commonly used in many applications. By using this function, developers can create command-line tools with clear and concise user interfaces, making them more accessible and user-friendly. With the help of the examples provided in this article, developers can easily integrate the flag.Bool() function into their applications and take advantage of its powerful capabilities.

Updated on: 18-Apr-2023

660 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements