On Thursday, 23 June 2016 20:27:21 UTC-4, Nick Pavlica wrote:
>
> While learning Go from the book "The Go Programming Language", by 
> Addison-Wesley I was working through an example and was taken back by how 
> easy it is to cause a data race.
>

I'm glad you appreciated this risk, but I hope it did not scared you away 
from concurrent programming in Go.  It takes some care and discipline to 
avoid data races, but a couple of simple rules and practices can greatly 
reduce the risk. (1) Avoid mutating variables where possible. Variables 
whose value is set once and then never updated are inherently 
concurrency-safe, as our functional programming friends have been saying 
for years. (2) Encapsulate variables.  By hiding variables so that all the 
functions that access them are under your control, you make it easier to 
ensure that they are not accessed concurrently.  For example, you can see 
that all accesses occur while a mutex lock is held.  Sometimes you can 
confine the variable to a single goroutine, avoiding concurrent access 
entirely.  You'll find that Chapter 9 of the book is devoted to the topic 
of data races and how to avoid them.


-- 
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