FAQ
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.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dan Kortschak at Jan 14, 2015 at 8:08 pm
    Add a Lock/Unlock pair of methods to A that call the methods on A.lock?
    On 15/01/2015, at 5:45 AM, "suraj" wrote:

    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.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/d/optout.
  • Larry Clapp at Jan 14, 2015 at 9:48 pm
    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
    On Wednesday, January 14, 2015 at 2:15:24 PM UTC-5, suraj wrote:

    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.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 14, '15 at 7:15p
activeJan 14, '15 at 9:48p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase