06丨数据类型

 06丨数据类型

bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32,represents a Unicode code point
float32 float64
complex64 complex128

类型转化

与其他主要编程语言的差异
1.Go语言不允许隐式类型转换
2.别名和原有类型也不能进行隐式类型转换

类型的预定义值

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

指针类型

与其他主要编程语言的差异
1.不支持指针运算
2. string是值类型,其默认的初始化值为空字符串,而不是nil

package type_test

import (
    "math"
    "testing"
)

type MyInt int64

func TestImplict(t *testing.T) {
    var a int32 = 1
    var b int64
    //b = a // 不能隐式转换cannot use a (type int32) as type int64 in assignment
    b = int64(a)  // 显示转换
    t.Log("b", b) // type_test.go:15: b 1

    var c MyInt
    //c=b //不支持
    c = MyInt(b) // 别名类型也要显示转换

    t.Log(a, b, c) //    type_test.go:21: 1 1 1

}

func TestPoint(t *testing.T) {
    a := 1
    aPtr := &a     // 获取a的指针
    t.Log(a, aPtr) //type_test.go:24: 1 0xc000094268
    //aPtr = aPtr + 1        // 指针不能进行运算
    t.Logf("%T %T", a, aPtr) //type_test.go:26: int *int

}

func TestString(t *testing.T) {
    var s string
    t.Log("*" + s + "*") // 初始化零值是"" 是空字符串而不是nil    //type_test.go:37: **
    t.Log(len(s))        //    type_test.go:37: 0

    num := math.MaxFloat64
    t.Log(num) //    type_test.go:40: 1.7976931348623157e+308
}

 

posted @ 2021-01-20 21:49  元贞  阅读(55)  评论(0)    收藏  举报