On Tue, Sep 5, 2017 at 10:34 AM, Marlon Che <rob198...@gmail.com> wrote:
> Hi everyone!
> Please help me figure out the two different results of following code:
>
> package main
>
> import (
>     "fmt"
>     "time"
> )
>
> func main() {
>     var num = 10
>     var p = &num
>
>     c := make(chan int)
>
>     go func() {
>         c <- *p // with this line we will get 11 from channel c
>         //c <- num // with this line we will get 10 from channel c
>     }()
>
>     time.Sleep(2 * time.Second)
>     num++
>     fmt.Println(<-c)
>
>     fmt.Println(p)
> }

You have a data race, what value you get from dereferencing p is
undefined, it could be 10, it could be 11, it could wipe your
harddrive or launch the missiles.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to