ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.