go 1.13.1 (linux x64): time.After not working as expected

169 views
Skip to first unread message

Jeff Kayser

unread,
Oct 14, 2019, 8:53:23 AM10/14/19
to golang-nuts
I'm working through the most excellent book "Concurrency in Go", doing the example on page 162-166.  My code is here:


The program is not triggering the timeout section of the code (lines 103-103 in the playground code).

All of the other examples have worked as expected, including the one just before this, which is almost identical, except for a couple of tweaks.  This is the first one that hasn't worked.

What am I doing wrong?

~Jeff Kayser

Steven Hartland

unread,
Oct 14, 2019, 9:04:04 AM10/14/19
to Jeff Kayser, golang-nuts
Your not checking done in your for loop.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6316c901-9425-4d4f-9bb3-2f7d4aed94a5%40googlegroups.com.

Jeff Kayser

unread,
Oct 14, 2019, 11:51:35 AM10/14/19
to golang-nuts
Forgot to increment the variable "i" (the loop counter).  Doh!  My bad.  I found it.  Thanks for looking over my buggy code.
After I fixed that, I also discovered that, by ending the loop after two iterations, the goroutine ended, closed the channels, and then the main program detected the channel closing, rather than the timeout.  Lovely.  
As Benny Hill would say: "Learning all the time".

The book "Concurrency in Go" is absolutely fabulous, second only to The Go Programming Language.
We are so lucky to have all those good Go books.

Thanks again.

Jeff Kayser

Reply all
Reply to author
Forward
0 new messages