隐藏结构字段并使其同步字段的访问和修改的最佳方法是什么?

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 ?

You don't have to add a mutex to the values you marshal, that's pointless.

But you do need to use the User's mutex while you copy or set its fields.

Some important things:

  • If you embed a non-pointer mutex, you must specify all your methods with pointer receiver, else the lock will be copied!
  • You do not need to specify json tags on unexported fields, that's redundant. And going further, since you provide your own marshaling logic, you don't even have to provide any json tags, since they won't be used at all. So this User is perfectly enough:

    type User struct {
        name  string
        email string
        sync.RWMutex
    }
    
  • Even though name and email are unexported, those values are not "safe", as you provided an exported MarshalJSON() method which return those values (in JSON format). You still have compile-time safety about accessing User.name and User.email, but know that values they store are not secret.

Example:

func (u *User) MarshalJSON() ([]byte, error) {
    u.RLock()
    usr := struct {
        Name  string `json:"name"`
        Email string `json:"email,omitempty"`
    }{u.name, u.email}
    u.RUnlock()

    return json.Marshal(usr)
}

func (u *User) UnmarshalJSON(b []byte) error {
    usr := struct {
        Name  string `json:"name"`
        Email string `json:"email"`
    }{}

    if err := json.Unmarshal(b, &usr); err != nil {
        return err
    }

    u.Lock()
    u.name = usr.Name
    u.email = usr.Email
    u.Unlock()

    return nil
}