dsh125986083 2018-09-27 09:05
浏览 70
已采纳

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

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 ?

  • 写回答

1条回答 默认 最新

  • dtzk85937 2018-09-27 09:11
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)