pupuk / blog

My New Blog. Record & Share. Focus on PHP, MySQL, Javascript and Golang.
MIT License
9 stars 2 forks source link

Go语言小计 琐碎点 基础点 #36

Open pupuk opened 2 years ago

pupuk commented 2 years ago

string的底层

c语言中,一段连续的内存,再以\0结束。 go中 ,起点point指针,+ len属性(字符串字节长度),因此go中的string是二进制安全的。 image https://github.com/golang/go/blob/master/src/reflect/value.go#L2481

image https://github.com/golang/go/blob/master/src/runtime/string.go#L229 Go的string的底层和Redis String都底层有些差异,是值得学习的知识点。Redis的字符串称之为SDS(simple dynamic string) 后面补充这一块 image image 对字符串重新赋值后,咋一看,地址没有变化。其实: &str, 是指向str的地址,并不是str中真正存字符的array的地址。 image image 重新赋值以后,底层的地址变了

Fiber的源码中实现了,string和slice互转,但没有内存拷贝的情况。 image https://github.com/gofiber/fiber/blob/master/utils/convert.go#L15