// loop over an array/a slicefor i, e :=range a {// i is the index, e the element}// if you only need e:for _, e :=range a {// e is the element}// ...and if you only need the indexfor i :=range a {}// In Go pre-1.4, you'll get a compiler error if you'...
We can use the for loop in Golang to perform a while loop. For this, we will keep only the test condition of the loop which will control the entry to the loop. The initialization, in this case, will be done before the loop starts and the update will be made inside the body of th...
since t.hasher may panic,// in which case we have not actually done a write. h.flags ^= hashWritingif h.buckets == nil { h.buckets = newobject(t.bucket) // newarray(t.bucket, 1) }again:// 省略 ...bucketloop:// 省略 ....
We define an array of values. for _, num := range nums { sum += num } We iterate over the array with therangeclause. Therangereturns the index and the value in each iteration. Since we do not use the index, we specify the discard_operator. (The Golang documentation calls it the ...
}// 预先分配固定 Size 的池子p.workers=newWorkerArray(loopQueueType,size)}else{// 初始化不创建,运行时再创建p.workers=newWorkerArray(stackType,0)} p.cond=sync.NewCond(p.lock)// 开启一个goroutine清理过期的 workergo p.purgePeriodically()returnp,nil ...
=top{// 优化寻址,tophash数组复用了标志位,一旦检测到emptyRest则跳出外层大循环,返回key在map中未找到ifb.tophash[i]==emptyRest{breakbucketloop}// key的tophash和tophash数组中的某个元素不相等,则进入小循环的下一个index的对比,或者进入大循环的下一个桶链表的查找continue}// 走到这一步,就是tophash...
overLoadFactor(h.count+1, h.B) { bigger = 0 // 标记是等量扩容 h.flags |= sameSizeGrow } // 记录老桶数组 oldbuckets := h.buckets // 申请新的空间(翻倍或者不变) newbuckets, nextOverflow := makeBucketArray(t, h.B+bigger, nil) flags := h.flags &^ (iterator | oldIterator) if...
}// 获取一个随机的哈希种子h.hash0 = fastrand()// 确定B的大小B :=uint8(0)foroverLoadFactor(hint, B) { B++ } h.B = B// 分配桶ifh.B !=0{varnextOverflow *bmap h.buckets, nextOverflow = makeBucketArray(t, h.B,nil)ifnextOverflow !=nil{ ...
{ // The P got a new worker. // Exit this worker. return false } } return true }, unsafe.Pointer(park), "GC worker (idle)", traceEvGoBlock, 0) // 检查P的gcBgMarkWorker是否和当前的G一致, 不一致时结束当前的任务 // Loop until the P dies and disassociates this // worker (the...
h.buckets = newobject(t.bucket)// newarray(t.bucket, 1) } again: // 省略 ... bucketloop: // 省略 ... done: ifh.flags&hashWriting ==0{ fatal("concurrent map writes") } h.flags &^= hashWriting ift.indirectelem { elem = *((*unsafe.Pointer)(elem)) ...