FAQ
Hi,

I would like to implement the XML Marshaler and Unmarshaler interfaces. I
have read the documentation, but I am still clueless. Could somebody give
me some examples?

Thanks.

Henry

--
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Matt Harden at Jul 12, 2015 at 12:52 am
    Why do you want to implement those interfaces? What are you trying to
    accomplish?
    On Sat, Jul 11, 2015 at 3:12 PM Henry Adi Sumarto wrote:

    Hi,

    I would like to implement the XML Marshaler and Unmarshaler interfaces. I
    have read the documentation, but I am still clueless. Could somebody give
    me some examples?

    Thanks.

    Henry

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Henry Adi Sumarto at Jul 12, 2015 at 2:29 am
    I have an unexported struct that is exposed via an interface. There is no
    problem with serializing the struct to an XML, but it becomes a problem
    when deserializing the XML back to the struct since the struct itself is
    unexported and the compiler does not know how to how to build the interface
    object. Here is the illustration in codes:

    type Inner interface{
        //...
    }

    type Outer interface{
        InnerData []Inner
        XML() string
    }

    type innerImplementation struct{
        //...
    }

    type outerImplementation struct{
        //...
    }

    func (outer *outerImplementation) XML() string{
        //serialize using encoding/xml.Marshal
        //This is OK. The serializer knows how to serialize both Outer and Inner
    }

    func NewOuterFRomXML(xml string) Outer{
        //deserialize using encoding/xml.Unmarshal
        //This is not OK. The deserializer can deserialize Outer, but not Inner.
        //It makes sense because there is no way the deserializer knows how to
    build Inner.
        //Custom Unmarshaller implementation for Inner is needed.
    }


    Hence, that's why I need to learn how to implement the Unmarshaler and
    Marshaler interfaces

    On Sunday, July 12, 2015 at 7:52:21 AM UTC+7, Matt Harden wrote:

    Why do you want to implement those interfaces? What are you trying to
    accomplish?

    On Sat, Jul 11, 2015 at 3:12 PM Henry Adi Sumarto <henry.ad...@gmail.com
    <javascript:>> wrote:
    Hi,

    I would like to implement the XML Marshaler and Unmarshaler interfaces. I
    have read the documentation, but I am still clueless. Could somebody give
    me some examples?

    Thanks.

    Henry

    --
    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 golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matt Harden at Jul 12, 2015 at 2:39 pm
    The Outer type you showed is not valid in Go. Interfaces can't have data
    members. It feels like you're trying to do object oriented programming in
    Go, and you've only given a very generic description of what you're trying
    to do.

    There are 744 examples of UnmarshalXML on GitHub -
    https://github.com/search?l=go&q=UnmarshalXML&type=Code
    On Sat, Jul 11, 2015 at 9:29 PM Henry Adi Sumarto wrote:

    I have an unexported struct that is exposed via an interface. There is no
    problem with serializing the struct to an XML, but it becomes a problem
    when deserializing the XML back to the struct since the struct itself is
    unexported and the compiler does not know how to how to build the interface
    object. Here is the illustration in codes:

    type Inner interface{
    //...
    }

    type Outer interface{
    InnerData []Inner
    XML() string
    }

    type innerImplementation struct{
    //...
    }

    type outerImplementation struct{
    //...
    }

    func (outer *outerImplementation) XML() string{
    //serialize using encoding/xml.Marshal
    //This is OK. The serializer knows how to serialize both Outer and
    Inner
    }

    func NewOuterFRomXML(xml string) Outer{
    //deserialize using encoding/xml.Unmarshal
    //This is not OK. The deserializer can deserialize Outer, but not
    Inner.
    //It makes sense because there is no way the deserializer knows how to
    build Inner.
    //Custom Unmarshaller implementation for Inner is needed.
    }


    Hence, that's why I need to learn how to implement the Unmarshaler and
    Marshaler interfaces

    On Sunday, July 12, 2015 at 7:52:21 AM UTC+7, Matt Harden wrote:

    Why do you want to implement those interfaces? What are you trying to
    accomplish?

    On Sat, Jul 11, 2015 at 3:12 PM Henry Adi Sumarto <henry.ad...@gmail.com>
    wrote:
    Hi,
    I would like to implement the XML Marshaler and Unmarshaler interfaces.
    I have read the documentation, but I am still clueless. Could somebody give
    me some examples?

    Thanks.

    Henry

    --
    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 golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 11, '15 at 8:12p
activeJul 12, '15 at 2:39p
posts4
users2
websitegolang.org

2 users in discussion

Henry Adi Sumarto: 2 posts Matt Harden: 2 posts

People

Translate

site design / logo © 2021 Grokbase