Golang中的常用数据结构

原文链接:https://www.ahaoaha.top/2019/05/17/Golang中的常用数据结构/

string

golang中string是一个内置类型,它的默认值是"",golang中的string的底层结构与C++的STL中的string是很类似的,golang中string是定长的,在底层结构的len中记录当前string的长度,它不支持扩容,但是golang依旧支持string的+/+=操作。

type stringStruct struct {
	str unsafe.Pointer	//指向一个[len]byte的数组
	len int	//代表string的长度
}
string赋值

当一个stirng赋值给另一个string时,仅仅是对结构体中的Pointer以及len进行值拷贝,这时候两个string对象就会指向同一块内存,很容易会想到浅拷贝的常见问题,但是实际上并不会出现浅拷贝的问题,通常string的内存会被编译器分配到只读区,该内存区域是不可写入的,当对字符串赋值时,对应字符串结构体中的Pointer会被修改为另一个地址,不会影响前一个字符串结构体中的地址

s1 := "hello"
s2 := s1
s2 = "h"
//此时s2的Pointer与s1的Pointer不同

使用fmt.Sprintf生成的字符串会被分配在堆空间

+操作

根据gdb调试发现+操作会调用runtime/string.go中的concatstring2函数

func concatstrings(buf *tmpBuf, a []string) string {
	idx := 0
	l := 0
	count := 0
	for i, x := range a {
		n := len(x)
		if n == 0 {
			continue
		}
		if l+n < l {
			throw("string concatenation too long")
		}
		l += n	//计算出两个字符串len的和
		count++	//保存要拼接的字符串的个数
		idx = i	//保存要拼接的最后一个字符串的index
	}
	if count == 0 {	//表示传进来的字符串slice为空
		return ""
	}

	// If there is just one string and either it is not on the stack
	// or our result does not escape the calling frame (buf != nil),
	// then we can return that string directly.
	if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {	//count=1代表需要拼接的字符串只有一个,如果当前字符串位于只读区且buf不为nil则可以直接返回
		return a[idx]
	}
	s, b := rawstringtmp(buf, l)	//s指向string,此时的s长度为l,b指向s的Pointer内存缓冲区
	for _, x := range a {	//此时b string的长度即为a中的所有的string的长度总和,该步将a中的所有字符按顺序拷贝到buf中
		copy(b, x)
		b = b[len(x):]
	}
	return s
}

func concatstring2(buf *tmpBuf, a [2]string) string {
	return concatstrings(buf, a[:])
}

array

var 数组名 [数组元素个数]数组元素类型{...}定义数组,定义array与slice的区别就在于是否包含明确的元素个数,若有则为数组,数组不支持扩容

type array struct {
    array unsafe.Pointer
    len int
}

slice

slice与C++的STL中的vector类似,是一种动态的数组,

type slice struct {
    array unsafe.Pointer
    len int
    cap int
}
//slice append扩容机制
func growslice(et *_type, old slice, cap int) slice {
	if raceenabled {
		callerpc := getcallerpc()
		racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, funcPC(growslice))
	}
	if msanenabled {
		msanread(old.array, uintptr(old.len*int(et.size)))
	}

	if cap < old.cap {
		panic(errorString("growslice: cap out of range"))
	}

	if et.size == 0 {
		// append should not create a slice with nil pointer but non-zero len.
		// We assume that append doesn't need to preserve old.array in this case.
		return slice{unsafe.Pointer(&zerobase), old.len, cap}
	}

	newcap := old.cap
	doublecap := newcap + newcap
	if cap > doublecap {
		newcap = cap
	} else {
		if old.len < 1024 {	//旧元素个数小于1024则按照2倍扩容
			newcap = doublecap
		} else {
			// Check 0 < newcap to detect overflow
			// and prevent an infinite loop.
			for 0 < newcap && newcap < cap {
				newcap += newcap / 4	//大于1024按照1.25倍扩容
			}
			// Set newcap to the requested cap when
			// the newcap calculation overflowed.
			if newcap <= 0 {
				newcap = cap
			}
		}
	}

	var overflow bool
	var lenmem, newlenmem, capmem uintptr
	// Specialize for common values of et.size.
	// For 1 we don't need any division/multiplication.
	// For sys.PtrSize, compiler will optimize division/multiplication into a shift by a constant.
	// For powers of 2, use a variable shift.
	switch {
	case et.size == 1:
		lenmem = uintptr(old.len)
		newlenmem = uintptr(cap)
		capmem = roundupsize(uintptr(newcap))
		overflow = uintptr(newcap) > maxAlloc
		newcap = int(capmem)
	case et.size == sys.PtrSize:
		lenmem = uintptr(old.len) * sys.PtrSize
		newlenmem = uintptr(cap) * sys.PtrSize
		capmem = roundupsize(uintptr(newcap) * sys.PtrSize)
		overflow = uintptr(newcap) > maxAlloc/sys.PtrSize
		newcap = int(capmem / sys.PtrSize)
	case isPowerOfTwo(et.size):
		var shift uintptr
		if sys.PtrSize == 8 {
			// Mask shift for better code generation.
			shift = uintptr(sys.Ctz64(uint64(et.size))) & 63
		} else {
			shift = uintptr(sys.Ctz32(uint32(et.size))) & 31
		}
		lenmem = uintptr(old.len) << shift
		newlenmem = uintptr(cap) << shift
		capmem = roundupsize(uintptr(newcap) << shift)
		overflow = uintptr(newcap) > (maxAlloc >> shift)
		newcap = int(capmem >> shift)
	default:
		lenmem = uintptr(old.len) * et.size
		newlenmem = uintptr(cap) * et.size
		capmem, overflow = math.MulUintptr(et.size, uintptr(newcap))
		capmem = roundupsize(capmem)
		newcap = int(capmem / et.size)
	}

	// The check of overflow in addition to capmem > maxAlloc is needed
	// to prevent an overflow which can be used to trigger a segfault
	// on 32bit architectures with this example program:
	//
	// type T [1<<27 + 1]int64
	//
	// var d T
	// var s []T
	//
	// func main() {
	//   s = append(s, d, d, d, d)
	//   print(len(s), "\n")
	// }
	if overflow || capmem > maxAlloc {
		panic(errorString("growslice: cap out of range"))
	}

	var p unsafe.Pointer
	if et.kind&kindNoPointers != 0 {
		p = mallocgc(capmem, nil, false)
		// The append() that calls growslice is going to overwrite from old.len to cap (which will be the new length).
		// Only clear the part that will not be overwritten.
		memclrNoHeapPointers(add(p, newlenmem), capmem-newlenmem)
	} else {
		// Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
		p = mallocgc(capmem, et, true)
		if writeBarrier.enabled {
			// Only shade the pointers in old.array since we know the destination slice p
			// only contains nil pointers because it has been cleared during alloc.
			bulkBarrierPreWriteSrcOnly(uintptr(p), uintptr(old.array), lenmem)
		}
	}
	memmove(p, old.array, lenmem)

	return slice{p, old.len, newcap}	//返回的是一个新的slice对象
}
  • 元素个数小于1024按照2倍扩容
  • 元素个数大于等于1024按照1.25倍扩容

map

声明方式:

  • var 对象名 map[key_type]valuetype:仅仅进行声明会创建一个nil map,nil map是不能用来存放键值对的,也就是不能使用的
  • 对象名 := make(map[key_type]value_type):可以直接使用
  • map_obj_name := map[key_type][value_type]{“key1”:“value1”, “key2”: “value2”, …}

查看map中是否有某个key值

value, ok := map_obj_name["key_name"]
//如果key_name存在,则返回对应的value和true
//如果key_name不存在,则返回nil和false
插入删除map中的元素
//插入元素:直接赋值,若key不存在则插入
map_obj_name[key_name] = value
//删除元素
delete(map_obj_name[key_name])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值