Home >Backend Development >Golang >How to Convert a Boolean to a String in Go?

How to Convert a Boolean to a String in Go?

Susan Sarandon
Susan SarandonOriginal
2024-12-06 00:36:11211browse

How to Convert a Boolean to a String in Go?

Convert Boolean to String in Go

Converting a boolean value to a string in Go requires a straightforward approach using the strconv package. The strconv.FormatBool(v) function provides an idiomatic way to achieve this conversion.

The strconv.FormatBool function takes a Boolean value as an argument and returns a string representation of that value. If the input value is true, it returns the string "true". If the input value is false, it returns the string "false".

To illustrate its usage, consider the following code example:

package main

import "fmt"
import "strconv"

func main() {
  // Define a boolean value
  isExist := true

  // Convert the boolean value to a string
  strIsExist := strconv.FormatBool(isExist)

  // Print the string representation
  fmt.Println(strIsExist) // Output: "true"
}

In this example, we define a boolean value isExist and then use the strconv.FormatBool function to convert it to a string. The resulting string strIsExist is printed to the console, and in this case, it will output "true".

The above is the detailed content of How to Convert a Boolean to a String in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn