flag.Bool() Function in Golang With Examples Last Updated : 19 May, 2020 Comments Improve Suggest changes Like Article Like Report 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: C // 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) } 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: C // 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) } 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 Comment More infoAdvertise with us Next Article flag.Bool() Function in Golang With Examples S sayantanm19 Follow Improve Article Tags : Go Language Golang-Misc Similar Reads reflect.Bool() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Bool() Function in Golang is used to get Value underlying value. To access this function, one needs to imports th 1 min read bits.Len() Function in Golang with Examples Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides Len() function which is used to find the minimum number of bits required to represent a and the result is 2 min read fmt.Scan() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Scan() function in Go language scans the input texts which is given in the standard input, reads from there and stores the successive space-separated values into successive arg 2 min read bits.Sub() Function in Golang with Examples The bits.Sub() Function in Golang is used to find the difference of a, b and borrow, i.e. diff = a - b - borrow. Here the borrow must be 0 or 1; otherwise, the behavior is undefined. To access this function, one needs to imports the math/bits package in the program. The return value of the borrowOut 2 min read complx.NaN() Function in Golang With Examples complx.NaN() function in Golang is used to returns a complex ânot-a-numberâ value. Syntax: func NaN() complex128 It returns the complex NaN value. Let us discuss this concept with the help of given examples: Example 1: C // Golang program to illustrate // the complx.NaN() Function package main impor 1 min read Like