Home > Backend Development > Golang > golang bool type conversion

golang bool type conversion

WBOY
Release: 2023-05-15 11:42:37
Original
1393 people have browsed it

In Golang, the bool type is a Boolean type with only two values: true and false. In some cases, we need to convert bool type to other types, such as integer, string, etc. So, how to convert bool type in Golang? The following are detailed instructions:

  1. bool conversion to integer type

In Golang, the bool type can be easily converted to an integer type. We can implement this process using if and else statements. For example, the following code converts the bool value true to the integer value 1 and the bool value false to the integer value 0:

var b bool = true
var i int
if b {
    i = 1
} else {
    i = 0
}
fmt.Println(i) // 输出 1
Copy after login
  1. Integer to bool

In Golang, integer types can also be easily converted to bool types. We can use the equality operator (==) to achieve this process. For example, the following code converts the integer value 1 to the bool value true and the integer value 0 to the bool value false:

var i int = 1
var b bool = i == 1
fmt.Println(b) // 输出 true
Copy after login
  1. bool to string

In Golang, the bool type can be converted to a string type. We can use the FormatBool() function in the strconv package to achieve this process. For example, the following code converts the bool value true to the string "true" and the bool value false to the string "false":

import "strconv"

var b bool = true
var s string = strconv.FormatBool(b)
fmt.Println(s) // 输出 "true"
Copy after login
  1. String to bool

In Golang, the string type can also be converted to the bool type. We can use the ParseBool() function in the strconv package to implement this process. For example, the following code converts the string "true" to the bool value true, and the string "false" to the bool value false:

import "strconv"

var s string = "true"
var b bool, err = strconv.ParseBool(s)
fmt.Println(b) // 输出 true
Copy after login

It should be noted that if the string is not "true" or "false ", the ParseBool() function will return an error.

To sum up, bool type conversion in Golang is very simple and easy to use. You only need to master the corresponding functions and keywords. It should be noted that frequent type conversion may affect the performance of the program, so it should be used with caution during implementation.

The above is the detailed content of golang bool type conversion. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template