gob on structure having mutex

529 views
Skip to first unread message

suraj

unread,
Jan 14, 2015, 2:15:24 PM1/14/15
to golan...@googlegroups.com
Hi, 

Gob encoding for below structure does not work :
type A struct {
  sync.RWMutex
  ID       int 
  Value string 
because sync.RWMutex does not have any exported field.

If I do:
type A struct {
  lock sync.RWMutex
  ID       int 
  Value string
}
now it works, as lock is a private field.

But then I have to do A.lock.Lock(), which doesn't sound very correct. 

Other approach can be :
type B struct {
   A
   sync.RWMutex
}

Can you please tell me if there is a better approach to this? As both look difficult to justify, unless you know its due to requirement of gob encoding.

Dan Kortschak

unread,
Jan 14, 2015, 3:08:23 PM1/14/15
to suraj, golan...@googlegroups.com
Add a Lock/Unlock pair of methods to A that call the methods on A.lock?

Larry Clapp

unread,
Jan 14, 2015, 4:48:33 PM1/14/15
to golan...@googlegroups.com
The "Example (EncodeDecode)" in the encoding/gob documentation has an example which encodes & decodes unexported fields.  You should be able to use that to figure out how to encode & decode your type A while ignoring the RWMutex.

-- L
Reply all
Reply to author
Forward
0 new messages