2016-07-24 38 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),因爲你會爲幾乎所有類型。

相關問題