請考慮以下代碼c := 90d := 101a := int64( (float64(c) / float64(d)) * 100) fmt.Println(a) fmt.Println(string(a))請考慮以下代碼c := 90d := 101a := int64( (float64(c) / float64(d)) * 100) fmt.Println(a) fmt.Println(string(a))這是1.16個游樂場返回./prog.go:13:21: conversion from int64 to string yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?)在我的本地1.13中,它返回_也不是我所期望的89有人可以給我一個提示,為什么這是行為,以及將int64轉換為字符串的推薦方法是什么?返回./prog.go:13:21: conversion from int64 to string yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?)在我的本地1.13中,它返回_也不是我所期望的89有人可以給我一個提示,為什么這是行為,以及將int64轉換為字符串的推薦方法是什么?
1 回答

LEATH
TA貢獻1936條經驗 獲得超7個贊
有關轉換的 Go 規(guī)范 部分將“從有符號或無符號整數類型轉換為 ”“定義為生成一個字符串,該字符串包含由整數值指定的單個 Unicode 符文的 UTF-8 表示形式。因此,正如《贊美詩:迪斯科》在注釋中指出的那樣,首先將89轉換為相應的Unicode符文,該符文只是拉丁文大寫字母Y,然后以UTF-8編碼此符文,產生字符串 。(為了得到你需要。string
string(89)
"Y"
_
string(95)
正如本文檔同樣指出的那樣,將產生與 相同的結果,并產生 或 。string(0xf8)
"?"
"\xc3\xb8"
string(0x6535)
"日"
"\xe6\x97\xa5"
很多Go用戶對此感到驚訝,期待生產.事實上,你需要或類似的東西來做到這一點:另請參閱 string() 做我希望 strconv 做的事情。Itoa() 會做的。Rob Pike本人建議從Go 2中刪除string(integer_value
)。正如Cerise Limón在鏈接的問題中指出的那樣,這就是為什么現(xiàn)在指出這個常見的錯誤。string(89)
"89"
strconv.Itoa
fmt.Sprint
go vet
- 1 回答
- 0 關注
- 224 瀏覽
添加回答
舉報
0/150
提交
取消