Home >Backend Development >Golang >golang bool to string

golang bool to string

王林
王林Original
2023-05-10 15:57:372322browse

In the Golang Go language, the conversion between Boolean and string is a problem that often needs to be dealt with. The Boolean type represents two values ​​of true or false, while the string type is a data type that can store any characters. This article will discuss how to convert Boolean type into string type, and introduce several processing methods in Golang.

In Golang, the Boolean type is defined with the bool keyword, which has only two values: true and false. The string type is represented by double quotes or backticks, such as "hello world" or This is a multi-line string, but this article mainly introduces how to convert bool type data into a string .

1. fmt.Sprintf() function

fmt.Sprintf() function is a commonly used formatted output function in the Go language, which supports various data types. Convert to string. When you need to convert the Boolean type to a string type, you can use the following format:

str := fmt.Sprintf("%t", b)

where %t is the placeholder for converting the Boolean type to a string, b is the Boolean type data that needs to be converted. The sample code is as follows:

package main

import (
    "fmt"
)

func main() {
    b1 := true
    b2 := false

    str1 := fmt.Sprintf("%t", b1)
    str2 := fmt.Sprintf("%t", b2)

    fmt.Println(str1, str2)
}

The output result is:

true false

2. strconv.FormatBool() function

strconv.FormatBool() The function is Go A function used in the language to convert Boolean type into string type. It accepts a bool type data as an input parameter and returns the corresponding string type data. The sample code is as follows:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    b1 := true
    b2 := false

    str1 := strconv.FormatBool(b1)
    str2 := strconv.FormatBool(b2)

    fmt.Println(str1, str2)
}

The output result is:

true false

3. Direct conversion

In Golang, Boolean type and numeric type can be converted to each other, where true is converted to a number It is 1 when type, false when converted to numeric type, it is 0. Therefore, when converting a Boolean type to a string, you can also convert the Boolean type to a numeric type, and then convert the numeric type to a string type. The sample code is as follows:

package main

import (
    "fmt"
)

func main() {
    b1 := true
    b2 := false

    str1 := fmt.Sprintf("%d", b1)
    str2 := fmt.Sprintf("%d", b2)

    fmt.Println(str1, str2)
}

The output result is:

1 0

4. Implement conversion by yourself

In addition to using the above function, we can also implement data type conversion by ourselves . The sample code is as follows:

package main

import (
    "fmt"
)

func BoolToStr(b bool) string {
    if b {
        return "true"
    }
    return "false"
}

func main() {
    b1 := true
    b2 := false

    str1 := BoolToStr(b1)
    str2 := BoolToStr(b2)

    fmt.Println(str1, str2)
}

The output result is:

true false

Summary

The above are several ways to convert Boolean into string type. In actual development, we need to choose an appropriate method to convert between Boolean type data and string type data according to the specific situation.

The above is the detailed content of golang bool to string. 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