步骤一:Chan的收发
// 上报用的chan
var reportChan = make(chan string, 2000)
// 上报接口API
func UserTraceReportAPI(message string) {
reportChan <- message
}
// 启动GoRoutine来上报
func startGoRoutineToReport() {
go func(){
for {
reportMessage := <- reportChan
fmt.Printf("startGoRoutineToReport: %v\n", reportMessage )
}
}()
}
func main() {
startGoRoutineToReport() // 启动GoRoutine来上报
for i:=0; i<1000; i++ {
fmt.Printf("i is %v\n", i)
UserTraceReportAPI(time.Now().String)
time.Sleep(time.Second)
}
}
网友评论