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
Release: 2024-12-06 00:36:11
Original
211 people have browsed it

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"
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template