FAQ
Thanks, the WaitGroup was exactly what I was trying to achieve with this
test.



Benziane Chakib
FrontEnd Engineer
Tel: +49 176 78983903
Twitter: @sp4ke <https://twitter.com/sp4ke>
Github: sp4ke <https://github.com/sp4ke>
BTC: 18RirJMHTNDXokNb3G4ytCadnm7pdUp8vX
On Sat, Mar 7, 2015 at 12:32 PM, Egon wrote:


On Saturday, 7 March 2015 12:03:34 UTC+2, Chakib Benziane wrote:

I am trying to understand go channels/select. This is a modification of
the go tour chapter about buffered channel creation.

I want this code to produce the output:
1
2
3
quit

But I am getting this instead
1
2
quit
That shouldn't happen, and it isn't happening for me.

package main

import (
"fmt"
"time"
)


func main() {
c := make(chan int, 3)
c <- 1
c <- 2


tick := time.Tick(2000 * time.Millisecond)


go func(c chan int) {
for v := range c {
fmt.Println(v)
}
fmt.Println("quit")
}(c)


select {
case <-tick:
c <- 3
close(c)
}
}

This code doesn't wait for the go routine to be finished. Go program exits
when main finishes.

You can see this by adding a sleep at the end of the program.
http://play.golang.org/p/OlvLjIo9Bq
Of course using a WaitGroup in practical programs would be a better idea.

+ Egon



--
You received this message because you are subscribed to a topic in the
Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/golang-nuts/HHNHzOj-l9s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 5 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 7, '15 at 10:03a
activeMar 7, '15 at 7:13p
posts5
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase