The program is really racy, but the result is also really some 
counter-intuitive.
The following program also print 10, which means evaluation of pointer 
dereference
is some different to evaluation of other expressions in flow.

package main

import (
    "fmt"
    "time"
)

func main() {
    var num = 10
    var p = &num

    c := make(chan int)

    go func() {
        c <- func()int{return *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(time.Second)
    num++
    fmt.Println(<-c)

    fmt.Println(p)
}

On Monday, September 4, 2017 at 10:35:32 PM UTC-4, Jesse McNelis wrote:
>
> On Tue, Sep 5, 2017 at 10:34 AM, Marlon Che <rob1...@gmail.com 
> <javascript:>> 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