2016-07-24 15 views

Odpowiedz

23

użyciu pakietu StrConv

docs

strconv.FormatBool(v)

func FormatBool (b Bool) łańcuch FormatBool zwraca "true" lub "false"
według wartość b

4

możesz użyć strconv.FormatBool tak:

package main 

import "fmt" 
import "strconv" 

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

lub można użyć fmt.Sprint takiego:

package main 

import "fmt" 

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

lub pisać jak strconv.FormatBool:

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

Wystarczy użyć fmt.Sprintf("%v", isExist), tak jak w przypadku niemal wszystkich typów .