03-数据类型

87 阅读1分钟

基本数据类型

  • 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

类型转换

  1. 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)
}
  1. 别名和原有类型也不能进行隐式类型转换
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)
}

类型的预定值

  1. math.MaxInt64
  2. math.MaxFloat64
  3. math.MaxUint32

总结

  1. Go语言不支持任何类型的隐式转换,且不支持类型别名的隐式转换
  2. Go语言支持指针类型,但是不支持指针运算的
func TestPoint(t *testing.T) {
    a := 1
    aPtr := &a
    // aPtr += 1 这会报错
    t.Log(a, aPtr)
    t.Logf("%T %T", a, aPtr)
}
  1. Go语言的字符串类型是值类型,默认初始化的空字符串而不是空
func TestString(t *testing.T)  {
    var s string
    t.Log("*" + s + "*")
    t.Log(len(s))
    if s == ""{
            // 判空操作
    }
}