Golang encoding/xml.MarshalerAttr type example

package encoding/xml

MarshalerAttr is the interface implemented by objects that can marshal themselves into valid XML attributes.

MarshalXMLAttr returns an XML attribute with the encoded value of the receiver. Using name as the attribute name is not required, but doing so will enable Unmarshal to match the attribute to the correct struct field. If MarshalXMLAttr returns the zero attribute Attr{}, no attribute will be generated in the output. MarshalXMLAttr is used only for struct fields with the "attr" option in the field tag.

Golang encoding/xml.MarshalerAttr type usage example

 ...
 if fv.CanInterface() && fv.Type().Implements(marshalerAttrType) {
  attr, err := fv.Interface().(MarshalerAttr).MarshalXMLAttr(name) // <-- here
  if err != nil {
 return err
  }
  if attr.Name.Local != "" {
 start.Attr = append(start.Attr, attr)
  }
  continue
 }
 ...

Reference :

https://golang.org/src/pkg/encoding/xml/marshal.go

http://golang.org/pkg/encoding/xml/#MarshalerAttr

Advertisement