FAQ
Hello, I am new at Go and I read A tour of Go till i came to channels. The
example in tour works perfect, but i tried to modify it and found that
after my modification program does not print lines after using a "<-d" in
func named goroutine. Can anyone explain to me where i made a mistake ?
link of code: http://play.golang.org/p/7ypWML8DYY

--

Search Discussions

  • Kevin Gillette at Dec 30, 2012 at 3:31 am
    You created a race condition: as soon as func main returns, the program
    unconditionally exits (and any other goroutines are immediately killed).
    The output you were expecting to see would only occur if the goroutine
    "won" the race. All code run on the playground has GOMAXPROCS set to
    exactly 1 (you cannot change it), and because the scheduler is
    deterministic, and in this case, favors continuing code in the main
    goroutine even after `d <- 1` completes, then the child goroutine can never
    win.

    In <http://play.golang.org/p/dIKpo_rrs4>, I added a scheduling point with
    time.Sleep, which will allow the other goroutine to run. It's still 'racy',
    because the goroutines aren't explicitly communicating after the output you
    wanted to see, but on the playground (and in practice), it should generally
    work.
    On Saturday, December 29, 2012 11:14:28 AM UTC-7, gedm...@gmail.com wrote:

    Hello, I am new at Go and I read A tour of Go till i came to channels. The
    example in tour works perfect, but i tried to modify it and found that
    after my modification program does not print lines after using a "<-d" in
    func named goroutine. Can anyone explain to me where i made a mistake ?
    link of code: http://play.golang.org/p/7ypWML8DYY
    --
  • Gedminast at Dec 30, 2012 at 5:29 pm
    Ok, thank you. Now I understood my mistake. I have another question related
    to channels. Is it possible to change variable value received through
    channel and print changed value in the func where the variable was sent
    from, like in java CSP (one2one or any2one channels). I don't know if you
    can understand my question so i added link to playground trying to show the
    problem: http://play.golang.org/p/6eyr78OAKU So is it possible to do that
    or is it only solution to pass that value back through channel?

    --
  • Bryanturley at Dec 31, 2012 at 2:18 am
    http://play.golang.org/p/y7nKo9vLuQ
    Your code did stuff that was confusing to me so I just illustrated the
    question.

    Have you only written in java previous to go?
    If so you are going to have to learn how to use pointers.

    http://en.wikipedia.org/wiki/Pointer_%28computer_programming%29 a start
    On Sunday, December 30, 2012 10:27:08 AM UTC-6, gedm...@gmail.com wrote:

    Ok, thank you. Now I understood my mistake. I have another question
    related to channels. Is it possible to change variable value received
    through channel and print changed value in the func where the variable was
    sent from, like in java CSP (one2one or any2one channels). I don't know if
    you can understand my question so i added link to playground trying to show
    the problem: http://play.golang.org/p/6eyr78OAKU So is it possible to do
    that or is it only solution to pass that value back through channel?
    --
  • Gedminast at Dec 31, 2012 at 3:24 pm
    Yes, I had my program written in java, and now I am trying to write it in
    Go. Your suggested solution using pointers was the answer i wanted to know,
    Thank You:)
    I modified my previous code, and now it does what i wanted it to do:
    http://play.golang.org/p/y0WI8G-vrU


    2012 m. gruodis 31 d., pirmadienis 04:18:01 UTC+2, bryanturley rašė:
    http://play.golang.org/p/y7nKo9vLuQ
    Your code did stuff that was confusing to me so I just illustrated the
    question.

    Have you only written in java previous to go?
    If so you are going to have to learn how to use pointers.

    http://en.wikipedia.org/wiki/Pointer_%28computer_programming%29 a start
    On Sunday, December 30, 2012 10:27:08 AM UTC-6, gedm...@gmail.com wrote:

    Ok, thank you. Now I understood my mistake. I have another question
    related to channels. Is it possible to change variable value received
    through channel and print changed value in the func where the variable was
    sent from, like in java CSP (one2one or any2one channels). I don't know if
    you can understand my question so i added link to playground trying to show
    the problem: http://play.golang.org/p/6eyr78OAKU So is it possible to
    do that or is it only solution to pass that value back through channel?
    --
  • Dustin Sallings at Dec 31, 2012 at 5:54 pm

    gedminast@gmail.com writes:

    Yes, I had my program written in java, and now I am trying to write it
    in Go. Your suggested solution using pointers was the answer i wanted
    to know, Thank You:)
    I modified my previous code, and now it does what i wanted it to do:
    http://play.golang.org/p/y0WI8G-vrU
    It's really better to not try to synchronize execution by timing and
    luck. It looks like you wanted a simple application of WaitGroup here:

    http://play.golang.org/p/nlQjiov_vA

    --
    dustin

    --
  • Bryanturley at Jan 1, 2013 at 3:50 am

    On Monday, December 31, 2012 10:45:14 AM UTC-7, Dustin wrote:
    gedm...@gmail.com <javascript:> writes:
    Yes, I had my program written in java, and now I am trying to write it
    in Go. Your suggested solution using pointers was the answer i wanted
    to know, Thank You:)
    I modified my previous code, and now it does what i wanted it to do:
    http://play.golang.org/p/y0WI8G-vrU
    It's really better to not try to synchronize execution by timing and
    luck. It looks like you wanted a simple application of WaitGroup here:

    I would agree except here, where you don't want to introduce two new
    concepts when teaching one of them.
    time.Sleep() is easier to understand than WaitGroup, though in real code
    time.Sleep() would not be a good idea.
  • Dustin Sallings at Jan 1, 2013 at 8:14 pm
    bryanturley <bryanturley@gmail.com>
    writes:
    I would agree except here, where you don't want to introduce two new
    concepts when teaching one of them.
    time.Sleep() is easier to understand than WaitGroup, though in real
    code time.Sleep() would not be a good idea.
    The confusion was around goroutines exiting before expected. I don't
    think we help people by showing them how to reduce the probability of a
    race condition affecting them when there's an easy to use
    straightforward way to be sure when it's over.

    It'd also be acceptable to use a channel to signal the termination.

    I do think we shouldn't encourage people to write bugs, though.

    --
    dustin

    --
  • Bryanturley at Jan 2, 2013 at 8:14 pm

    On Tuesday, January 1, 2013 2:13:58 PM UTC-6, Dustin wrote:
    bryanturley <bryan...@gmail.com <javascript:>>
    writes:
    I would agree except here, where you don't want to introduce two new
    concepts when teaching one of them.
    time.Sleep() is easier to understand than WaitGroup, though in real
    code time.Sleep() would not be a good idea.
    The confusion was around goroutines exiting before expected. I don't
    think we help people by showing them how to reduce the probability of a
    race condition affecting them when there's an easy to use
    straightforward way to be sure when it's over.

    It'd also be acceptable to use a channel to signal the termination.

    I do think we shouldn't encourage people to write bugs, though.

    --
    dustin
    Looking back the code I posted it uses neither time.Sleep(), WaitGroup or a
    end sync channel...


    --
  • Tim Dufrane at Jan 2, 2013 at 8:41 pm
    I don't think that sleeping or waiting is necessary for any of it. You can
    achieve this simply by putting a blocking channel at the end of the
    goroutine: http://play.golang.org/p/SX2abAkv4b
    On Monday, December 31, 2012 12:45:14 PM UTC-5, Dustin wrote:

    gedm...@gmail.com <javascript:> writes:
    Yes, I had my program written in java, and now I am trying to write it
    in Go. Your suggested solution using pointers was the answer i wanted
    to know, Thank You:)
    I modified my previous code, and now it does what i wanted it to do:
    http://play.golang.org/p/y0WI8G-vrU
    It's really better to not try to synchronize execution by timing and
    luck. It looks like you wanted a simple application of WaitGroup here:

    http://play.golang.org/p/nlQjiov_vA

    --
    dustin
    --
  • Tim Dufrane at Jan 2, 2013 at 7:57 pm
    http://play.golang.org/p/7TNQDxNbkL - is that the behavior you intended?
    On Saturday, December 29, 2012 1:14:28 PM UTC-5, gedm...@gmail.com wrote:

    Hello, I am new at Go and I read A tour of Go till i came to channels. The
    example in tour works perfect, but i tried to modify it and found that
    after my modification program does not print lines after using a "<-d" in
    func named goroutine. Can anyone explain to me where i made a mistake ?
    link of code: http://play.golang.org/p/7ypWML8DYY
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 30, '12 at 2:52a
activeJan 2, '13 at 8:41p
posts11
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase