2016-11-26 10 views

回答

2

Go中没有隐式转换。为了比较或使用不同类型的两个值进行算术运算,您必须执行手动和显式类型转换。

a := 3   // numerical constant 3 defaults to int 
b := uint(2) 
c := a < b  // compiler error 
d := a < int(b) // OK 
2

Go只使用explicit type conversions进行包括比较在内的所有操作。

var a uint64 
var b int64 

a = 1 
b = 1 
if a == b { 
    fmt.Println("Equal") 
} 

这个片段将会导致错误:

tmp/sandbox428200464/main.go:13: invalid operation: a == b (mismatched types uint64 and int64)

做比较,你必须投中的变量同类型明确:

if int64(a) == b {...} 

所以,绝对,如果可以这样说,它是固定的。

Playgound为片段。