2016-07-24 94 views

回答

23

使用STRCONV包

docs

strconv.FormatBool(v)

FUNC FormatBool(B布尔)串FormatBool根据b

的值返回 “true” 或 “假”
4

您可以使用strconv.FormatBool这样的:

package main 

import "fmt" 
import "strconv" 

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

,或者您可以使用fmt.Sprint这样的:

package main 

import "fmt" 

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

或写这样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

只需使用fmt.Sprintf("%v", isExist),因为你会为几乎所有类型。