基本数据类型
- bool
- string
- int、int8、int6、int32、int64
- uint、uint8、uint6、uint32、uint64、uintptr
- byte
alias for uint8
- rune
for int32,represents a Unicode code point
- float32、float64
- complex64、complex128
类型转换
- Go语言不允许隐式类型转换
package type_test
import "testing"
func TestImplicit(t *testing.T) {
var a int32 = 1
var b int64
b = int64(a)
t.Log(a, b)
}
- 别名和原有类型也不能进行隐式类型转换
package type_test
import "testing"
type MyInt int64
func TestImplicit(t *testing.T) {
var a int32 = 1
var b int64
b = int64(a)
var c MyInt
c = MyInt(b)
t.Log(a, b, c)
}
类型的预定值
math.MaxInt64
math.MaxFloat64
math.MaxUint32
总结
- Go语言不支持任何类型的隐式转换,且不支持类型别名的隐式转换
- Go语言支持指针类型,但是不支持指针运算的
func TestPoint(t *testing.T) {
a := 1
aPtr := &a
t.Log(a, aPtr)
t.Logf("%T %T", a, aPtr)
}
- Go语言的字符串类型是值类型,默认初始化的空字符串而不是空
func TestString(t *testing.T) {
var s string
t.Log("*" + s + "*")
t.Log(len(s))
if s == ""{
}
}