3 ms·
The purpose of channels is to provide synchronisation and messaging between goroutines, not to prevent race conditions on shared data. Go also provides mutexes
by mytailorisrich 7y ago
The purpose of channels is to provide synchronisation and messaging between goroutines, not to prevent race conditions on shared data.
Go also provides mutexes that are more suited for that.
Sure, passing a pointer through a channel does not in itself provide any protection to the data it references but such protection is not always required. There's a separation of concerns that allows more flexibility.
- erik_seaberg 7y ago> such protection is not always required Safety should be the default, with a way to opt out for people who have thought really hard about the consequences. If I have to opt into the safe version, I need tooling to complain about everywhere I didn't do so.
- mytailorisrich 7y agoA pointer is neither safe nor unsafe and in my view people should be expected to know what they are doing when passing pointers across concurrent threads of execution. It's not rocket science. Goroutines are supposed to be lightweight so it makes sense to allow lightweight communication.
- qtplatypus 7y agoOn the other hand how often have you read in documentation “Do not mutate this buffer” or the equivalent? It is very possible to create traps for people who pick up your code later on. Having the compiler detect those problems rather then it causing a runtime error is something highly desirable.