Besides what others already suggested, you'll find the reason in the
Go memory model specification:
https://golang.org/ref/mem
This part is relevant here:
"If the effects of a goroutine must be observed by another goroutine,
use a synchronization mechanism such as a lock or channel
communication to establish a relative ordering."

+1:
Instead of html in email, please use only text. Although in this
particular case the playground links would have been just enough.
(If you are curious, my email reader renders the code text with light
yellow on white background. )

On 7/5/19, White Pure <wu.purewh...@gmail.com> wrote:
> Thanks for your reply.
> I've tried before and it reported race.
> But I don't understand why this code has race, and what's the possible bug?
> Here's the output:
> $ go run -race main.go
> ==================
> WARNING: DATA RACE
> Write at 0x00c000096000 by goroutine 7:
>   main.main.func3()
>       /Users/purewhite/go/src/xxx/main.go:26 +0x46
>
> Previous read at 0x00c000096000 by goroutine 5:
>   sync/atomic.LoadInt32()
>       /usr/local/Cellar/go/1.12.6/libexec/src/runtime/race_amd64.s:206 +0xb
>   main.main.func1()
>       /Users/purewhite/go/src/xxx/main.go:13 +0x38
>
> Goroutine 7 (running) created at:
>   main.main()
>       /Users/purewhite/go/src/xxx/main.go:23 +0x115
>
> Goroutine 5 (running) created at:
>   main.main()
>       /Users/purewhite/go/src/xxx/main.go:11 +0xbd
> ==================
> Found 1 data race(s)
> exit status 66
>
>
> 在 2019年7月5日星期五 UTC+8下午6:48:11,Andrew Pillar写道:
>>
>> Have you tried building with the -race flag?
>>
>>   go build - race
>>
>> This should help you figure out any data race conditions that may occur.
>>
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/9c57fc61-141b-40b5-947a-5d7a3ad3547e%40googlegroups.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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CA%2Bctqrqq014qUTX%3DE5gZrN8x5vxvkdh3f8GPeKf%3D2gFLKb3CLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to