If I am using channels properly, should I need to use mutexes to protect against concurrent access?
相关问题
- Golang mongodb aggregation
- Why it isn't advised to call the release() met
- How to flatten out a nested json structure in go
- how to install private repo using glide golang
- Managing data-store concurrency as microservices s
相关文章
- Can I run a single test in a suite?
- How to check if a request was cancelled
- Is it possible to implement an interface with unex
- How to access value of first index of array in Go
- Embedded Interface
- Is there an existing solution for these particular
- How to represent an array with mixed types
- How does the piggybacking of current thread variab
You don't need mutex if you use channels correctly. In some cases a solution with mutex might be simpler though.
Just make sure the variable(s) holding the channel values are properly initialized before multiple goroutines try to access the channel variables. Once this is done, accessing the channels (e.g. sending values to or receiving values from them) is safe by design.
Supporting documents with references (emphases added by me):
Spec: Channel types:
Effective Go: Concurrency: Share by communicating
This article is also very helpful: The Go Memory Model
Also quoting from the package doc of
sync
: