Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a bool to a string in Go?

People also ask

How do you convert something to string in go?

You can convert numbers to strings by using the strconv. Itoa method from the strconv package in the Go standard libary. If you pass either a number or a variable into the parentheses of the method, that numeric value will be converted into a string value.

Can bool be converted to int?

bool values are convertible to int type, with true converting to 1 and false converting to 0 . This is guaranteed by the language.

What is boolean Golang?

Boolean values are those which can be assigned true or false and has the type bool with it. In the code above “bVal” is not initialized and thus has a zero-value. The zero-value for a boolean is false.

Can you convert a string to a bool in C++?

Using equality operator Finally, for a task as simple as this is, we can write our own validator using the equality operator, as shown below. But like the previous function, this also sets the boolean value to false on any invalid input. That's all about converting a string to bool value in C++.


use the strconv package

docs

strconv.FormatBool(v)

func FormatBool(b bool) string FormatBool returns "true" or "false"
according to the value of b


The two main options are:

  1. strconv.FormatBool(bool) string
  2. fmt.Sprintf(string, bool) string with the "%t" or "%v" formatters.

Note that strconv.FormatBool(...) is considerably faster than fmt.Sprintf(...) as demonstrated by the following benchmarks:

func Benchmark_StrconvFormatBool(b *testing.B) {
  for i := 0; i < b.N; i++ {
    strconv.FormatBool(true)  // => "true"
    strconv.FormatBool(false) // => "false"
  }
}

func Benchmark_FmtSprintfT(b *testing.B) {
  for i := 0; i < b.N; i++ {
    fmt.Sprintf("%t", true)  // => "true"
    fmt.Sprintf("%t", false) // => "false"
  }
}

func Benchmark_FmtSprintfV(b *testing.B) {
  for i := 0; i < b.N; i++ {
    fmt.Sprintf("%v", true)  // => "true"
    fmt.Sprintf("%v", false) // => "false"
  }
}

Run as:

$ go test -bench=. ./boolstr_test.go 
goos: darwin
goarch: amd64
Benchmark_StrconvFormatBool-8       2000000000           0.30 ns/op
Benchmark_FmtSprintfT-8             10000000           130 ns/op
Benchmark_FmtSprintfV-8             10000000           130 ns/op
PASS
ok      command-line-arguments  3.531s

In efficiency is not too much of an issue, but genericity is, just use fmt.Sprintf("%v", isExist), as you would for almost all the types.


you may use strconv.FormatBool like this:

package main

import "fmt"
import "strconv"

func main() {
    isExist := true
    str := strconv.FormatBool(isExist)
    fmt.Println(str)        //true
    fmt.Printf("%q\n", str) //"true"
}

or you may use fmt.Sprint like this:

package main

import "fmt"

func main() {
    isExist := true
    str := fmt.Sprint(isExist)
    fmt.Println(str)        //true
    fmt.Printf("%q\n", str) //"true"
}

or write like strconv.FormatBool:

// FormatBool returns "true" or "false" according to the value of b
func FormatBool(b bool) string {
    if b {
        return "true"
    }
    return "false"
}