* Yuan Ting <yuant...@ict.ac.cn> [200301 23:50]: > I write a simple program likes below and it triggers a data race alarm with > -race flag. But no matter how I run, this program will not panic. I know it > is legal to receive messages from nil channels, but I don't quite > understand why this program does not panic in the presence of data races. > > ch := make(chan struct{}) > go func() { > ch = nil > }() > go func() { > <-ch > }()
I think the other responses missed the obvious here (or at least chose more elaborate, though correct, explanations). This is a variable assignment in one goroutine concurrent with a variable reference in another. It is exactly the same (from the point of view of whether it is a race or not) as this: var a = 17 go func() { a = 987654 }() go func() { fmt.Println(a) } In the second goroutine, the variable a might have the value 17, it might have the value 987654, and it might have garbage that is neither of these. This is a race condition, and it is also true of the channel code above. One of the other responses very correctly said that a race does not imply that the code could panic. The integer code above will never panic, even on architectures where a write of an integer to memory concurrent with a read from that memory can possibly allow the read to return partially written data. However, the above code is clearly a race condition. ...Marvin -- 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/20200302133637.3iolxy54426rppgv%40basil.wdw.