Because you have a concurrent read/write to a. Specifically, the Memory model says that "the go statement that starts a new goroutine happens before the goroutine's execution begins" <https://golang.org/ref/mem#tmp_5>. This means the first code is fine - the write happens before the go statement, which happens before execution of f begins, which happens before the `print(a)`. So the write happens before the read and everything is fine. If you switch the two lines, the go statement still happens before execution of f begins and thus before `print(a)`. But now, it *also* happens before the write ("Within a single goroutine, reads and writes must behave as if they executed in the order specified by the program"). So the read and the write are concurrent - the go statement happens before both, but they are not ordered between them.
FTR, this should also be fairly obvious without looking at the specifics of the memory model. If you do go f() a = "hello world" You start a new goroutine reading `a`, so it should be clear, that when the assignment happens, there is a separate goroutine running that might read from it at the same time. On Tue, Feb 23, 2021 at 8:51 AM sanye <xba...@gmail.com> wrote: > Hello Gophers,a question bothers me while reading the doc: The Go Memory > Model > > package main > > var a string > > func f() { > print(a) > } > > func hello() { > a = "hello, world\n" > go f() > } > > func main() { > hello() > } > > This code snippet is OK > > But if I exchange the 2 lines in function hello, DATA RACE happens, why? > > -- > 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/5b622597-6c1c-e32c-7c18-a8dab525e322%40gmail.com > . > -- 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/CAEkBMfGMx0Q4aTax-PEpS%2B%3DiOvr65CuXRgS%2BiEqL%2B2201UE9yQ%40mail.gmail.com.