初级会员
  • 第 12867 位会员
  • fengweiqiang
  • 2017-09-30 05:11:47
  • Offline
  • 19 91

最近发布的文章

    暂无

最近分享的资源

    暂无

最近发布的项目

    暂无

最近的评论

  • #4 @939496716 嗯嗯,谢谢,学习了
  • #1 @sonoho timer2 再定义一个的话没法执行逻辑代码吧![3.png](https://static.studygolang.com/190925/00dc87f4e4fe9d54ae393163943b5fa5.png)
  • 评论了博文 golang context使用说明
    How can I completely terminate go func()? ``` package main import ( "context" "log" "time" ) func longRunningCalculation(timeCost int)chan string{ result:=make(chan string) go func (){ time.Sleep(time.Second*(time.Duration(timeCost))) log.Println("Still doing other things...")//Even if it times out, this goroutine is still doing other tasks. result<-"Done" log.Println(timeCost) }() return result } func jobWithTimeout(){ ctx,cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() select{ case <-ctx.Done(): log.Println(ctx.Err()) return case result:=<-longRunningCalculation(3): log.Println(result) } } func main() { jobWithTimeout() time.Sleep(time.Second*5) } ``` 2019/09/25 11:00:16 context deadline exceeded 2019/09/25 11:00:17 Still doing other things...
  • 在线等啊,求帮忙