Golang strconv.FormatBool() Function with Examples

Golang | strconv.FormatBool() Function: Here, we are going to learn about the FormatBool() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 07, 2021

strconv.FormatBool()

The FormatBool() function is an inbuilt function of the strconv package which is used to get "true" or "false" according to the given value of b. Where b is the bool type. Or, we can say that the FormatBool() function is used to convert a bool value to a string.

It accepts one parameter (b) and returns "true" or "false" according to the value of b.

Syntax:

func FormatBool(b bool) string

Parameter(s):

  • b : A bool value which is to be used to get "true" or "false".

Return Value:

The return type of FormatBool() function is bool, it returns "true" or "false" according to the given value.

Example 1:

// Golang program to demonstrate the
// example of strconv.FormatBool() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.FormatBool(true))
	fmt.Println(strconv.FormatBool(false))
}

Output:

true
false

Example 2:

// Golang program to demonstrate the
// example of strconv.FormatBool() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var x bool
	var result string

	x = true
	result = strconv.FormatBool(x)
	fmt.Printf("x (Type: %T, Value: %v)\n", x, x)
	fmt.Printf("result (Type: %T, Value: %q)\n", result, result)

	x = false
	result = strconv.FormatBool(x)
	fmt.Printf("x (Type: %T, Value: %v)\n", x, x)
	fmt.Printf("result (Type: %T, Value: %q)\n", result, result)
}

Output:

x (Type: bool, Value: true)
result (Type: string, Value: "true")
x (Type: bool, Value: false)
result (Type: string, Value: "false")

Example 3:

// Golang program to demonstrate the
// example of strconv.FormatBool() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	yes := true
	no := false

	fmt.Println("Is she knowledgeable?", strconv.FormatBool(yes))
	fmt.Println("Is she healthy?", strconv.FormatBool(no))
}

Output:

Is she knowledgeable? true
Is she healthy? false

Golang strconv Package »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.