func mapdelete(t *maptype, h *hmap, key unsafe.Pointer) {
if raceenabled && h != nil {
callerpc := getcallerpc()
pc := funcPC(mapdelete)
racewritepc(unsafe.Pointer(h), callerpc, pc)
raceReadObjectPC(t.key, key, callerpc, pc)
}
if msanenabled && h != nil {
msanread(key, t.key.size)
}
if h == nil || h.count == 0 {
if t.hashMightPanic() {
t.key.alg.hash(key, 0) // see issue 23734
}
return
}
if h.flags&hashWriting != 0 {
throw("concurrent map writes")
}
//go语言可以选择hash和equals的不同实现
alg := t.key.alg
//根据key获取hash值
hash := alg.hash(key, uintptr(h.hash0))
// Set hashWriting after calling alg.hash, since alg.hash may panic,
// in which case we have not actually done a write (delete).
//走到这一步说明我们对key计算hash值的时候没有发生panic,所以后边我们可以标识我们正在对
//此map进行操作
h.flags ^= hashWriting
//bucketMask(h.B)方法是生成B个1的二进制方法,比如B为5的话,则buc
go语言的map结构delete方法源码解读
最新推荐文章于 2024-03-21 17:36:11 发布
本文深入探讨了Go语言中map的delete方法,详细解析了其内部实现,包括哈希表的查找、删除过程,以及在并发环境下如何保证数据安全。通过对源码的分析,帮助读者理解Go语言map删除操作的底层逻辑。
摘要由CSDN通过智能技术生成