ardanlabs / service

Starter-kit for writing services in Go using Kubernetes.

Home Page:https://www.ardanlabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: why value semantics in worker mutex?

mrkagelui opened this issue · comments

Hi, one of the most valuable lesson I've learned in your teaching is consistent semantics for a given struct, but for mutexes, the source code uses pointer semantics, why does the worker package use value semantics? is it because we don't want two worker structs possibly share a mutex?

When you make a copy of a mutex, the copy is a different mutex. Locking one copy doesn't lock the other.

The answer is not to use pointer semantics at the field level. Use value semantics for the mutex field and use pointer semantics for the struct.

Plus, once you have a mutex, the struct represents an API. Use pointer semantics for structs that represent an API.

got it, I think that makes sense.