Jun 262020

ADM

How to convert a boolean to a string in Golang

  • 26 June 2020
  • ADM

 

 

 

How to convert a boolean to a string in Golang - images/logos/golang.jpg

 

Here is a simple snippet how to convert a boolean to a string in Golang. If you need help how to install Golang check the references links.

Code

FormatBool method returns "true" or "false" according to the value of b.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	/** boolean variable */
	b := true

	/** converting the b variable into a string */
	str := strconv.FormatBool(b)

	/** displaying the string variable into the console */
	fmt.Println(str)
}

Compile&Run

To compile the code navigate to the file location and run the following command.

$ go build bool_to_string.go

Then depending if you are on Linux or windows the binary file is created.

to run the application execute the command.

Linux

$ bool_to_string

Windows

c:\Users\adm\go\tutorials> bool_to_string.exe

If you want to compile and run the application in one single step run the following command:

go run bool_to_string.go

Output

Will simple output true.

true

 

 

 

References