Here is a problem I am facing with a golang struct
type User struct {
name string `json:"name"`
email string `json:"email"`
}
Now I want the access and modification of this struct fields to be concurrent safe And hence have added a mutex and added methods which locks the mutex The user code can now access and mutate only via methods and cannot directly access the fields
type User struct {
name string `json:"name"`
email string `json:"email"`
sync.RWMutex `json:"-"`
}
func (u *User) Name() string {
u.RLock()
defer u.RUnlock()
return u.name
}
func (u *User) Email() string {
u.RLock()
defer u.RUnlock()
return u.email
}
func (u *User) SetName(p string) {
u.Lock()
defer u.Unlock()
u.name = p
}
func (u *User) SetEmail(p string) {
u.RLock()
defer u.RUnlock()
u.email = p
}
So far so good but the problem is json/bson marshalling fails as it requires exported fields
So I implement custom marshalling which returns a similar struct but with exported fields
func (self User) MarshalJSON() ([]byte, error) {
var usr struct {
Name string `json:"name"`
Email string `json:"email,omitempty"`
sync.RWMutex `json:"-"`
}
return json.Marshal(usr)
}
func (self *User) UnmarshalJSON(b []byte) error {
var usr struct {
Name string `json:"name"`
Email string `json:"email"`
sync.RWMutex `json:"-"`
}
if err := json.Unmarshal(b, &usr); err != nil {
return err
}
self.name = usr.Name
self.email = usr.Email
return nil
}
But this does not completely make the User struct concurrency safe as the marhsaling code is not locked.
My question is how to make the marshalling code to use the same mutex? Making the mutex global is not going to solve the problem as we create multiple instances of the struct. The user struct declared in marshaling is different from the main User struct so locking on the mutex of inner struct is meaningless.
What's the best way to achieve this ?