-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Embedding a sync.Mutex is equally bad whether or not the type is exported #127
Comments
Hey, @peterbourgon, thanks for calling this out! We're in agreement that We left it there as an allowance for cases where engineers were embedding the That said, we agree that maybe that nuance is difficult to communicate. We're |
This was also inline with mentions in official Go talks, e.g., The export modified was added to avoid the The current guideline also clashes with #118 -- using exported methods as a hint that it's part of the API vs an internal method. An embedded mutex will export the |
Our guidance for mutexes suggested that it was okay to embed mutexes in structs if the struct was unexported. This was left as an allowance for "if you really want to do it, this is the only time it's okay", but that nuance doesn't translate well to the style guide. Advise against embedding mutexes completely to resolve any confusion here. Resolves #127
Our guidance for mutexes suggested that it was okay to embed mutexes in structs if the struct was unexported. This was left as an allowance for "if you really want to do it, this is the only time it's okay", but that nuance doesn't translate well to the style guide. Advise against embedding mutexes completely to resolve any confusion here. Resolves #127
Nice! 😎
😧 |
What makes unexported types special? 🙃
A mutex in a struct is almost always an implementation detail, used to provide concurrency-safety to the capabilities (methods) of the type. Embedding it allows callers to manipulate it directly, but that's an abstraction leak, breaks encapsulation, and is just an enormous maintenance risk, for what I hope are obvious reasons. Whatever callers are doing when they take and release the lock can equally well be expressed as a method, right?
Is there some other use case you have that motivates this exception?
The text was updated successfully, but these errors were encountered: