Standard library / encoding/xml

encoding/xml.Unmarshaler

Custom element decoding interface. UnmarshalXML must consume exactly one element; DecodeElement is a useful implementation strategy. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
type Unmarshaler interface {  UnmarshalXML(d *Decoder, start StartElement) error}

Working Example

Example Snippetgo
package main
import (  "encoding/xml"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
type label string
func (v *label) UnmarshalXML(d *xml.Decoder, s xml.StartElement) error {  var tmp string  if e := d.DecodeElement(&tmp, &s); e != nil {    return e  }  *v = label(tmp)  return nil}func main() { var v label; must(xml.Unmarshal([]byte("<a>Go</a>"), &v)); fmt.Println(v) }
Go Reference