FAQ
Hi, please help with newbie question, my code:package main

package main

import "fmt"
import "strconv"

func main() {
     ch := make(chan string)
     count := 2
for i := 0; i < count; i++ {
go get(ch)
}
add(ch)
     for{}
}

func get(ch chan string) {
for {
i := <-ch
fmt.Println("get", i)
}
}

func add(ch chan string) {
for i := 0; i < 4; i++ {
t := strconv.FormatInt(int64(i), 10)
ch <- t
fmt.Println("add", t)
}
}

if count = 2 output:
1) get 0 add 0 add 1 add 2 get 1 get 3 get 2 add 3 as expected

if count = 3 output:
2) get 0 add 0 add 1 add 2 add 3

if channel is buffered ( ch := make(chan string, 10)) output:
3) add 0 add 1 add 2 add 3

But I expected everywhere result number 1





--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Nigel Tao at Jul 10, 2013 at 4:55 am

    On Tue, Jul 9, 2013 at 8:49 PM, wrote:
    func main() {
    ch := make(chan string)
    count := 2
    for i := 0; i < count; i++ {
    go get(ch)
    }
    add(ch)
    for{}
    }
    The for{} loop at the end of the main function is a busy-loop.
    Goroutines are currently co-operatively scheduled, and this goroutine
    never yields, so other goroutines that are able to call fmt.Println
    are never run. Change
    for{}
    to
    select{}
    to yield the main goroutine so that others can run to completion.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vlad L. at Jul 10, 2013 at 7:58 am
    Great thanks

    Середа, 10 липня 2013 р. 07:55:54 UTC+3 користувач Nigel Tao написав:
    On Tue, Jul 9, 2013 at 8:49 PM, <[email protected] <javascript:>> wrote:
    func main() {
    ch := make(chan string)
    count := 2
    for i := 0; i < count; i++ {
    go get(ch)
    }
    add(ch)
    for{}
    }
    The for{} loop at the end of the main function is a busy-loop.
    Goroutines are currently co-operatively scheduled, and this goroutine
    never yields, so other goroutines that are able to call fmt.Println
    are never run. Change
    for{}
    to
    select{}
    to yield the main goroutine so that others can run to completion.
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 10, '13 at 2:36a
activeJul 10, '13 at 7:58a
posts3
users2
websitegolang.org

2 users in discussion

Vlad L.: 2 posts Nigel Tao: 1 post

People

Translate

site design / logo © 2023 Grokbase