golang日常记录

  • golang中 map slice interface chan 传递的是指针,是因为其 实例中 存储的数据 是用的指针。
    eg: slice
    ````
    type slice struct {
    array unsafe.Pointer
    len int,
    cap int,
    }
传递的过程中也是复制的 struct
结果:
1.append不能影响原slice
2.json.Unmashal(buf, &slice)需要传入slice地址
3.一个slice占三个字节

* 赋值使用的是地址

var i int
i = 1

i的类型不能变,赋其他值 I的地址也不会变,有别于动态语言

* map中的struct 不能直接修改 struct的属性

m := map[string]Student{"people": {"name":"aaa"}}
m["people"].name = "bbb"

是错误的

// A header for a Go map.
type hmap struct {
// 元素个数,调用 len(map) 时,直接返回此值
count int
flags uint8
// buckets 的对数 log_2
B uint8
// overflow 的 bucket 近似数
noverflow uint16
// 计算 key 的哈希的时候会传入哈希函数
hash0 uint32
// 指向 buckets 数组,大小为 2^B
// 如果元素个数为0,就为 nil
buckets unsafe.Pointer
// 扩容的时候,buckets 长度会是 oldbuckets 的两倍
oldbuckets unsafe.Pointer
// 指示扩容进度,小于此地址的 buckets 迁移完成
nevacuate uintptr
extra *mapextra // optional fields
}
```
// todo 详解map

猜你喜欢

转载自www.cnblogs.com/wayland3/p/11992927.html