var result MyXMLClasserr := xml.Unmarshal(data, &result)
type Submesh struct {UsesSharedVertices bool `xml:"usesharedvertices;default:true"`}
Since this functionality didn't seem available, I implemented it myself.
Example of the usage:
type Foo struct {
Bar bool `xml:"bar,attr" default:"true"`
}
Whenever the attribute bar is ommited, it will be set to true by the parser. This works for any native type (and pointers).
In case anyone needs it, here is what i did.
(Changes in the following files (as of golang 1.1))
encoding/xml/typeinfo.go
Added field default Value to type fieldInfo:
type fieldInfo struct {
idx []intname stringxmlns stringdefaultValue stringflags fieldFlagsparents []string
}
Added assignement to default value in func structFieldInfo()
...
if i := strings.Index(tag, " "); i >= 0 {
finfo.xmlns, tag = tag[:i], tag[i+1:]
}
finfo.defaultValue = f.Tag.Get("default")
// Parse flags.
tokens := strings.Split(tag, ",")
...
encoding/xml/read.go
Added initial assignment to the default value where applicable
in func (p *Decoder) unmarshal(val reflect.Value, start *StartElement) error:
...
for i := range tinfo.fields {
finfo := &tinfo.fields[i]if finfo.defaultValue != "" {
copyValue(finfo.value(sv), []byte(finfo.defaultValue))
}switch finfo.flags & fMode {
...