This is a creation in Article, where the information may have evolved or changed.
Golang has a large number of integral types, but generally only use int or Uintt.
Package Mainimport ("FMT" "unsafe") Func main () {var i1 int8 = 1 //1 bytevar i2 int16 = 2//2 Bytevar i3 int32 = 3//4 Bytevar i4 Int64 = 4//8 Bytevar i5 int = 5 //32 bit: 4 64 bit: 8var I6 uint8 = 1 //1 Bytevar i7 uint16 = 2//2 Byteva R i8 uint32 = 3//4 Bytevar I9 UInt64 = 4//8 Bytevar i10 UINT = 5 //32-bit: 4 64-bit: 8var i11 byte = 1//equivalent to Uint8 1 Bytev Ar i12 rune = 3//equivalent to Int32 4 bytevar i13 uintptr = 6//32 bit: 4 64 bit: 8FMT. Println (unsafe. Sizeof (i13))//8fmt. Println (I1, I2, i3, I4, i5, I6, i7, i8, I9, i10, I11, I12, i13)//1 2 3 4 5 1 2 3 4 5 1 3 6}
The int type and int32 or int64 cannot be added directly, only the same type (the same type name) can be added and reduced, and Golang does not support implicit conversions.
Package MainFunc Main () {var i1 int = 1var i2 int32 = 2var i3 int64 = 3i4: = I1 + I2}
. \main.go:9:Invalidoperation:i1+ i2 (mismatchedtypesint andint32)
Use INT (variable name cast)
Package Main
Import(
"FMT"
)
FuncMain (){
var i1 int=1
var i2 int32=2
var i3 int64=3
i4: =i1+int(i2)//int32 cast to int
i5: =i1+int(i3)//int64 cast to int
FMT. Println (I4,i5)
}