FAQ
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


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)
   }
}



Thanks for your help and remarks

--
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

  • Egon at Mar 7, 2015 at 11:32 am

    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 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.
  • Nico at Mar 7, 2015 at 11:58 am
    The program exits as soon as main completes, i.e. after sending 3 and closing the channel. The program exits before the goroutine has a chance to read the 3.

    You could add an:

      fmt.Println("exiting main")

    at the end of main to confirm this.

    --
    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.
  • Chakib Benziane at Mar 7, 2015 at 12:34 pm
    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.
  • Damien Collard at Mar 7, 2015 at 7:13 pm
    When the tick is received, you send 3 on channel c and immediately close c
    and reach the end of your program while the go routine runs. So you have no
    guarantee that the program won't terminate before the go routine completes,
    which is what happens here.

    I'm new to go so this may not be the best ways, but I think you can either
    (1) define another completion channel on which the go routine will send a
    message and make the program wait for the message or (2) use sync.WaitGroup
    as follows:

    import (
         //...
         "sync"
    )

    func main() {
         var w sync.WaitGroup
         w.Add(1)

         //...

         go func(c chan int) {
             //...
             w.Done()
         }(c)

         //...

         select {
             //...
         }

         w.Wait()
    }

    Note that the sync group's Add() must be done before the go routine is
    started.


    Le samedi 7 mars 2015 11:03:34 UTC+1, Chakib Benziane a écrit :
    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


    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)
    }
    }



    Thanks for your help and remarks
    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
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