Golang Bool to String Type Casting [2 Methods]


GO

Author: Tuan Nguyen
Reviewer: Deepak Prasad

Different methods to convert golang bool to string

Data type is an important concept in programming. Data type specifies the size and type of variable values. Go is statically typed, meaning that once a variable type is defined, it can only store data of that type. Go has three basic data types:

  • bool: represents a boolean value and is either true or false
  • Numeric: represents integer types, floating point values, and complex types
  • string: represents a string value

In some cases, you may want to convert a boolean value into a string value.

  • You can use the strconv package's FormatBool() function to do that. FormatBool() returns "true" or "false" according to the value of boolean. func FormatBool(b bool) string: takes a bool value as input and return 'true' or 'false'
  • Another way is using fmt.Sprintf(string, bool) string with the "%t" or "%v" formatters. func Sprintf(format string, a ...any) string: Sprintf formats according to a format specifier and returns the resulting string.

 

NOTE:
In this tutorial we will use different types of formatting verbs such as %V, %s, %d and may be more so if you are not familiar with these verbs definition then I would suggest to first go through Different Formatting Functions in Golang

 

Method 1: Using FormatBool() function

Here is an example of how to use the FormatBool() function to convert a boolean to a string.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	v := true
	// convert bool to string
	s := strconv.FormatBool(v)

	// print the boolean variable
	fmt.Printf("%T, %v\n", v, v)

	// print the string variable
	fmt.Printf("%T, %v\n", s, s)
}

Output:

$ go run main.go
bool, true
string, true

When we print out the value of v and s variables, nothing changes. But we can see that, the type of v is bool while the type of s is string. If we try to compare v to string "true", the program can not be complied because it is invalid operation: cannot compare v == "true" (mismatched types bool and untyped string):

	if v == "true" {
		println("Error")
	}

Output:

invalid operation: v == "true" (mismatched types bool and untyped string)

 

Method 2: Using Sprintf() function

Sprintf() formats according to a format specifier and returns the resulting string. Here, a is of Interface type hence you can use this method to convert any type to string. I will demonstrate how to convert bool to string variable using Sprintf() function in the example below

package main

import (
	"fmt"
	"reflect"
)

func main() {
	boo := true
	// convert bool to string value
	str := fmt.Sprintf("%v", boo)
	fmt.Println(str)
	fmt.Println(reflect.TypeOf(str))
}

Output:

$ go run main.go
true
string

Summary

In this article, I give you some examples of converting a boolean value to a string. The strconv.FormatBool(...) is considerably faster than fmt.Sprintf(). If efficiency is not too much of an issue, but genericity is, just use fmt.Sprintf("%v", v), as you would for almost all the types.

 

References

https://pkg.go.dev/strconv
https://pkg.go.dev/strconv#FormatBool
https://pkg.go.dev/fmt#Sprintf
https://pkg.go.dev/fmt

 

Tuan Nguyen

Tuan Nguyen

He is proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise spanning these technologies, he develops robust solutions and implements efficient data processing and management strategies across various projects and platforms. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment