1// 请求失败造成 panic2funcmain(){3resp,err:=http.Get("https://api.ipify.org?format=json")4defer resp.Body.Close()// resp 可能为 nil,不能读取 Body5iferr!=nil{6fmt.Println(err)7return8}910body,err:=ioutil.ReadAll(resp.Body)11checkError(err)1213fmt.Println(string(body))14}1516fu...
$ go tool pprof http://127.0.0.1:6060/debug/pprof/profile?seconds=30Saved profilein/root/pprof/pprof.demo.samples.CPU.001.pb.gzFile:demoType:CPUTime:Dec24,2023at11:42am(CST)Duration:10s,Total samples=70ms(0.7%)Entering interactivemode(type"help"forcommands,"o"foroptions)(pprof) 1.1.2 ...
type slice struct { array unsafe.Pointer // 指向底层数组的指针lenint// 切片的长度capint// 切片的容量} Golang 官方文档声明:函数参数传参只有值传递一种方式。值传递方式会在调用函数时将实际参数拷贝一份传递到函数中,slice 参数被传递到函数中时,其 array、len 以及 cap 都被复制了一份,因此函数...
type RpcContext struct { Version string TransactionServiceGroup string ClientRole meta.TransactionRole ApplicationId string ClientId string ResourceSets *model.Set Session getty.Session } 当收到事务消息时,我们需要构造这样一个 RpcContext 供后续事务处理逻辑使用。所以,我们会构造下列 map 来缓存映射关系: var...
// in which case we have not actually done a write. h.flags ^= hashWriting ifh.buckets ==nil{ h.buckets = newobject(t.bucket)// newarray(t.bucket, 1) } again: // 省略 ... bucketloop: // 省略 ... done: ifh.flags&hashWriting ==0{ ...
所以和上面一样第一个slot就是checkmark heapBitsSetType(uintptr(x), size, dataSize, typ) if dataSize > typ.size { // Array allocation. If there are any // pointers, GC has to scan to the last // element. if typ.ptrdata != 0 { scanSize = dataSize - typ.size + typ.ptrdata } ...
即之前的slice := array[2:4]等价于slice := array[2:4:10],在Go 1.2中因为引入了新的语法,即支持第三个参数,可以调整切片的容量。 “第二个冒号引入容量值,该值必须小于或等于源切片或数组的容量” slice := array[2:4:7]的cap为 7-2=5 ...
// The append built-in function appends elements to the end of a slice. If // it has sufficient capacity, the destination is resliced to accommodate the // new elements. If it does not, a new underlying array will be allocated.
const plainError string panic(plainError("assignment to entry in nil map")) 1.5 删 delete(myMap,5) 执行delete 方法时,倘若 key 存在,则会从 map 中将对应的 key-value 对删除;倘若 key 不存在或 map 未初始化,则方法直接结束,不会产生显式提示. 1.6 遍历 遍历分为下面两种方式: for k,v := ra...
netpollcheckerr 方法会检查当前 pollDesc 对应的 fd 是否是正常的, // 通常来说 netpollcheckerr(pd, mode) == 0 是成立的,所以这里会执行 gopark // 把当前 goroutine 给 park 住,直至对应的 fd 上发生可读/可写或者其他『期待的』I/O 事件为止, // 然后 unpark 返回,在 gopark 内部会把当前 go...