我試圖通過使用string(isExist)
將bool
稱爲isExist
轉換爲string
(true
或false
),但它不起作用。在Go中做這個的慣用方法是什麼?如何在Go中將bool轉換爲字符串?
6
A
回答
23
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)
,因爲你會爲幾乎所有類型。
相關問題
- 1. 如何在Go中將[Size]字節轉換爲字符串?
- 2. 將字符串轉換爲bool - swift2
- 3. iOS將bool值轉換爲字符串
- 4. 無法將字符串轉換爲Bool
- 5. 如何在Go中將[] interface {}轉換爲[]字符串?
- 6. 如何將uint16轉換爲Go中的2字節字符串?
- 7. 如何將* url.URL轉換爲GO中的字符串,Google App Engine
- 8. 如何將bool true或false轉換爲字符串「True」或「False」
- 9. 使用Automapper在DataReader中將字符串轉換爲bool
- 10. 不能把字符串轉換爲bool
- 11. 如何在Go中直接將十六進制字符串轉換爲[]字節?
- 12. 如何將字符串轉換爲非字符串在matlab中
- 13. 將bool值轉換爲json.dumps()中的字符串
- 14. 如何將字節緩衝區中以空字符結尾的字符串轉換爲Go中的字符串?
- 15. 爲什麼這個Github項目將字符串轉換爲bool?
- 16. Go中的文本處理 - 如何將字符串轉換爲字節?
- 17. 將bigint轉換爲Go中的字符串
- 18. 如何將字符串轉換(轉換)爲blob?在ESQL中?
- 19. 如何在Go lang中轉換uint16中的字符串值?
- 20. 如何將字符串轉換爲Perl中的unicode字符串
- 21. 如何將字符串的字符串轉換爲字符?
- 22. 將字符串轉換爲字符串
- 23. 將字符串轉換爲字符串
- 24. 將字符串轉換爲字符串
- 25. 不能隱式地將類型bool轉換爲字符串
- 26. 將類型'字符串'隱式轉換爲'bool'
- 27. 使用nHibernate標準將字符串轉換爲bool標準
- 28. 無法將字符串類型轉換爲bool C++
- 29. 錯誤:無法將類型'字符串'隱式轉換爲'bool'
- 30. 不能將類型'字符串'轉換爲'bool'