利用反射,探究Go语言中的数据类型

41 阅读5分钟

Go共有多少数据类型?


package main

import (
	"fmt"
	"reflect"
)

func main() {

	for i := reflect.Invalid; i <= reflect.UnsafePointer; i++ {
		fmt.Printf("%d:%s\n", i, i.String())
	}
}

结果为:

0:invalid
1:bool
2:int
3:int8
4:int16
5:int32
6:int64
7:uint
8:uint8
9:uint16
10:uint32
11:uint64
12:uintptr
13:float32
14:float64
15:complex64
16:complex128
17:array
18:chan
19:func
20:interface
21:map
22:ptr
23:slice
24:string
25:struct
26:unsafe.Pointer

即Go中共有 27 种类型,包括invalid,unsafe.Pointer

对应源码为

// A Kind represents the specific kind of type that a Type represents.
// The zero Kind is not a valid kind.
type Kind uint

const (
	Invalid Kind = iota
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Uintptr
	Float32
	Float64
	Complex64
	Complex128
	Array
	Chan
	Func
	Interface
	Map
	Ptr
	Slice
	String
	Struct
	UnsafePointer
)



各种数据类型在内存中的结构


package main

import (
	"fmt"
	"reflect"
	"unsafe"
)

func main() {

	var (
		boolA    bool
		intB     int
		int8C    int8
		int16D   int16
		int32E   int32
		int64F   int64
		uintptrG uintptr

		float32H float32
		float64I float64

		arrayJ [3]int
		chanK  chan int
		// 函数 在go语言中是一等公民
		funcL func()

		// go语言中的泛型,可代表任何类型
		//interfaceM interface{}

		mapN           map[int]int
		ptrO           *int
		sliceP         []int
		stringQ        string
		structR        struct{}
		unsafaPointerS unsafe.Pointer
	)

	var list []interface{}

	list = append(list, boolA, intB, int8C, int16D, int32E, int64F, uintptrG, float32H, float64I, arrayJ, chanK, funcL, mapN, ptrO, sliceP, stringQ, structR, unsafaPointerS)

	for _, v := range list {

		// 1.各种数据类型在内存中的结构
		// 类型名称,存储大小,对齐方式
		fmt.Printf("类型名称:%s,存储大小:%d,对齐方式:%d\n", reflect.TypeOf(v).Kind(), reflect.TypeOf(v).Size(), reflect.TypeOf(v).Align())

	}

}


结果为:

类型名称:bool,存储大小:1,对齐方式:1
类型名称:int,存储大小:8,对齐方式:8
类型名称:int8,存储大小:1,对齐方式:1
类型名称:int16,存储大小:2,对齐方式:2
类型名称:int32,存储大小:4,对齐方式:4
类型名称:int64,存储大小:8,对齐方式:8
类型名称:uintptr,存储大小:8,对齐方式:8
类型名称:float32,存储大小:4,对齐方式:4
类型名称:float64,存储大小:8,对齐方式:8
类型名称:array,存储大小:24,对齐方式:8
类型名称:chan,存储大小:8,对齐方式:8
类型名称:func,存储大小:8,对齐方式:8
类型名称:map,存储大小:8,对齐方式:8
类型名称:ptr,存储大小:8,对齐方式:8
类型名称:slice,存储大小:24,对齐方式:8  //  slice底层结构体有三个字段:指向底层数组的指针(占8字节),长度(8字节),容量(8字节)
类型名称:string,存储大小:16,对齐方式:8  // string类型底层是一个指针(8字节),和一个长度字段(8字节)
类型名称:struct,存储大小:0,对齐方式:1
类型名称:unsafe.Pointer,存储大小:8,对齐方式:8

复合类型的内存大小


package main

import (
	"fmt"
	"reflect"
)

func main() {

	var (
		arrayJ [3]int
		chanK  chan int

		mapN   map[int]int
		ptrO   *int
		sliceP []int
	)

	var list []interface{}

	list = append(list, arrayJ, chanK, mapN, ptrO, sliceP)

	for _, v := range list {

		// 1.各种数据类型在内存中的结构
		// 类型名称,存储大小,对齐方式
		fmt.Printf("类型名称:%s,存储大小:%d,对齐方式:%d\n", reflect.TypeOf(v).Kind(), reflect.TypeOf(v).Size(), reflect.TypeOf(v).Align())

	}

	var (
		arrayJ2 [3]int8
		chanK2  chan int8
		mapN2   map[int8]int8
		ptrO2   *int8
		sliceP2 []int8
	)

	fmt.Printf("array: %d,%d\n", reflect.TypeOf(arrayJ).Size(), reflect.TypeOf(arrayJ2).Size())
	fmt.Printf("chan: %d,%d\n", reflect.TypeOf(chanK).Size(), reflect.TypeOf(chanK2).Size())
	fmt.Printf("map: %d,%d\n", reflect.TypeOf(mapN).Size(), reflect.TypeOf(mapN2).Size())
	fmt.Printf("ptr: %d,%d\n", reflect.TypeOf(ptrO).Size(), reflect.TypeOf(ptrO2).Size())
	fmt.Printf("slice: %d,%d\n", reflect.TypeOf(sliceP).Size(), reflect.TypeOf(sliceP2).Size())

}


结果为:

类型名称:array,存储大小:24,对齐方式:8
类型名称:chan,存储大小:8,对齐方式:8
类型名称:map,存储大小:8,对齐方式:8
类型名称:ptr,存储大小:8,对齐方式:8
类型名称:slice,存储大小:24,对齐方式:8
array: 24,3 // 根据其基本类型,乘以数组长度决定其内存占用 (int占用8个byte,int8只占一个byte )
chan: 8,8
map: 8,8
ptr: 8,8 // channel,map,pointer的值都是一个地址(地址只需要一个byte)


slice: 24,24 // slice的24,对应3个地址,即具体地址,长度信息,容量信息,各占8个字节

channel,map,slice传递的是一个地址信息,而不是像其他的类型是具体数值


字节对齐


有关内存对齐




哪些数据类型可以对比?


package main

import (
	"fmt"
	"reflect"
	"unsafe"
)

func main() {

	var (
		boolA    bool
		intB     int
		int8C    int8
		int16D   int16
		int32E   int32
		int64F   int64
		uintptrG uintptr

		float32H float32
		float64I float64

		arrayJ [3]int
		chanK  chan int
		// 函数 在go语言中是一等公民
		funcL func()

		// go语言中的泛型,可代表任何类型
		//interfaceM interface{}

		mapN           map[int]int
		ptrO           *int
		sliceP         []int
		stringQ        string
		structR        struct{}
		unsafaPointerS unsafe.Pointer
	)

	var list []interface{}

	list = append(list, boolA, intB, int8C, int16D, int32E, int64F, uintptrG, float32H, float64I, arrayJ, chanK, funcL, mapN, ptrO, sliceP, stringQ, structR, unsafaPointerS)

	for _, v := range list {

		// 是否可以对比
		fmt.Printf("类型名称:%s,是否可以对比:%t\n", reflect.TypeOf(v).Kind(), reflect.TypeOf(v).Comparable())

	}

}

结果为:

类型名称:bool,是否可以对比:true
类型名称:int,是否可以对比:true
类型名称:int8,是否可以对比:true
类型名称:int16,是否可以对比:true
类型名称:int32,是否可以对比:true
类型名称:int64,是否可以对比:true
类型名称:uintptr,是否可以对比:true
类型名称:float32,是否可以对比:true
类型名称:float64,是否可以对比:true
类型名称:array,是否可以对比:true
类型名称:chan,是否可以对比:true
类型名称:func,是否可以对比:false
类型名称:map,是否可以对比:false
类型名称:ptr,是否可以对比:true
类型名称:slice,是否可以对比:false
类型名称:string,是否可以对比:true
类型名称:struct,是否可以对比:true
类型名称:unsafe.Pointer,是否可以对比:true

可见,只有func,map,slice不可对比

slice和map的值为地址,而不是该地址对应的具体值,故而无法直接对比. 只能通过reflect.DeepEqual方式间接对比

为什么 channel 可以对比? 参见


func的值也是一个地址




数值类型和引用类型


func,slice,map,channel引用类型,

其余为 数值类型

值类型与引用类型,值传递与引用传递




参考自

走出新人误区,了解类型底层 - 从reflect谈谈Go语言的类型

Go语言基础22 反射reflect01