在下面的型別轉換代碼中:
package main
import "fmt"
const c = 3.2
const i = 3
func main() {
var f float64 = i * c // implicit conversion of i
fmt.Println(f)
}
i&c是允許隱式轉換的無型別常量。在執行乘法之前是否i有c隱式轉換?float64
下面的代碼應該演示型別轉換:
package main
import "fmt"
func main() {
// s := string(97) // works fine, but, Is this type conversion or type casting?
// because below line gives error - "cannot convert 97.2 (type untyped float) to type string"
s := string(97.2)
fmt.Println(s)
}
- 是
string(97)型別轉換還是型別轉換?因為string(97)是給"a"但不是"97",這是基于這個答案的型別轉換
uj5u.com熱心網友回復:
在下文中,常量的乘法是在編譯時完成的,然后將結果轉換為 float 并f以 float64 形式存盤。運行時沒有型別轉換。
var f float64 = i * c
以下不是有效的 Go 代碼,因此它不是轉換或強制轉換。
s := string(97.2)
Go 中沒有型別轉換。
如果你這樣做了:
s:=int(f)
其中f是浮點數,這是一種型別轉換,其中的值f被轉換為int并分配給s。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453002.html
