golang底层数据结构

golang内置类型的底层数据结构

slice切片

//[]int16
type = struct []int16 {
    int16 *array;
    int len;
    int cap;

//[]byte
type = struct []uint8 {
    uint8 *array;
    int len;
    int cap;
}
  • slice中 array 是一个指针,它指向的是一个Array
  • len 代表的是这个slice中的元素长度
  • cap 是slice的容量

对slice的赋值、以值作为函数参数时只拷贝1个指针、2个int

特性

slice的Array存储在连续内存上:

  1. 随机访问很快,适合用下标访问,缓存命中率会高。
  2. 动态扩容时会涉及到内存拷贝和开辟新内存,会带来gc压力、内存碎片化。
  3. 如果知道所需空间,提前分配cap是很好的。
  4. 新、老 slice 共用底层数组,对底层数组的更改都会影响到彼此。
  5. append可以掰断新老slice共用底层数组的关系。(不理解?可以参考扩容原理)

string字符串

type = struct string {
    uint8 *str;
    int len;
}

string的底层结构因平台而异,并且在未来的版本更新中可能发生改变。
对string的赋值、以值作为函数参数时只拷贝1个指针、1个int

map

//map[int16]byte
type = struct hash<int16, uint8> {
    int count;
    uint8 flags;
    uint8 B;
    uint16 noverflow;
    uint32 hash0;
    struct bucket<int16, uint8> *buckets;
    struct bucket<int16, uint8> *oldbuckets;
    uintptr nevacuate;
    runtime.mapextra *extra;
} *

interface接口类型

//interface
//无方法
type = struct runtime.eface {
    runtime._type *_type;
    void *data;
}

//interface
//有方法
type = struct runtime.iface {
    runtime.itab *tab;
    void *data;
}

// layout of Itab known to compilers
// allocated in non-garbage-collected memory
// Needs to be in sync with
// ../cmd/compile/internal/gc/reflect.go:/^func.dumptypestructs.
type itab struct {
	inter *interfacetype
	_type *_type
	hash  uint32 // copy of _type.hash. Used for type switches.
	_     [4]byte
	fun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
}

// Needs to be in sync with ../cmd/link/internal/ld/decodesym.go:/^func.commonsize,
// ../cmd/compile/internal/gc/reflect.go:/^func.dcommontype and
// ../reflect/type.go:/^type.rtype.
type _type struct {
	size       uintptr
	ptrdata    uintptr // size of memory prefix holding all pointers
	hash       uint32
	tflag      tflag
	align      uint8
	fieldalign uint8
	kind       uint8
	alg        *typeAlg
	// gcdata stores the GC type data for the garbage collector.
	// If the KindGCProg bit is set in kind, gcdata is a GC program.
	// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
	gcdata    *byte
	str       nameOff
	ptrToThis typeOff
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Golang 提供了一些底层数据结构,这些数据结构可以用于构建高效的程序。以下是一些常见的底层数据结构: 1. 数组(Arrays):在 Golang 中,数组是固定长度的数据结构,可以存储相同类型的元素。数组使用索引访问元素,具有快速的随机访问能力。 2. 切片(Slices):切片是一个动态长度的数组,可以根据需要进行扩展或收缩。切片是基于数组实现的,提供了更灵活的操作和更方便的使用。 3. 映射(Maps):映射是一种无序的键值对集合。它类似于字典或哈希表,通过键来访问值。Golang 的映射使用哈希表来实现,具有快速的查找和插入能力。 4. 链表(Linked Lists):链表是一种基本的数据结构,它由多个节点组成,每个节点包含一个值和一个指向下一个节点的指针。链表可以用于实现队列、栈和其他高级数据结构。 5. 栈(Stacks):栈是一种后进先出(LIFO)的数据结构,只能在栈顶进行插入和删除操作。Golang 中可以使用切片或链表实现栈。 6. 队列(Queues):队列是一种先进先出(FIFO)的数据结构,只能在队尾进行插入操作,在队头进行删除操作。Golang 中可以使用切片或链表实现队列。 7. 堆(Heaps):堆是一种特殊的二叉树,具有一些特定的性质。在 Golang 中,可以使用堆接口和堆包来实现最小堆或最大堆。 8. 树(Trees):树是一种非线性数据结构,由节点和边组成。树在计算机科学中有广泛的应用,如二叉树、AVL 树、红黑树等。 这些底层数据结构可以帮助开发者构建高效的程序,并在不同的应用场景中发挥作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值