本文代码部分基于dive-to-gosync-workshop的代码 Golang 的NewTimer方法调用后,生成的timer会放入最小堆,一个后台goroutine会扫描这个堆,将到时的timer进行回调和channel(下面代码的 c := make(chan Time,1) )写入 // NewTimer creates a new Timer that will send // the current time on its channel after at least durati…
介绍 go的闭包是一个很有用的东西.但是如果你不了解闭包是如何工作的,那么他也会给你带来一堆的bug.这里我会拿出Go In Action这本书的一部分代码,来说一说在使用闭包的时候可能遇到的坑.全部的代码在github上. 闭包的坑 首先看一段代码: search/search.go 29 // Launch a goroutine for each feed to find the results. 30 for _, feed := range feeds { 31 // Retrieve…
今天在编写代码的时候,遇到了一个莫名其妙的错误,debug了半天,发现这是一个非常典型且易犯的错误.记之 示例代码: package main import "fmt" type aa struct { x, y int } type bb struct { member aa } func main() { m := []*aa{} pool := []bb { { member: aa{x : ,y : ,}, }, { member: aa{x : ,y : ,}, }, { m…