Answer the question
In order to leave comments, you need to log in
How to create xml with golang attributes?
Like from
type MyData struct {
Name string
Attributes map[string]string
}
<name atr1="v1" atr2="v2"/>
Answer the question
In order to leave comments, you need to log in
Something like this: https://play.golang.com/p/TqlsfIV2BSF
package main
import (
"encoding/xml"
"fmt"
)
type MyData struct {
XMLName xml.Name
Attributes []xml.Attr `xml:",attr"`
}
func main() {
v := MyData{
XMLName: xml.Name{Local: "somename"},
Attributes: []xml.Attr{
{Name: xml.Name{Local: "attr1"}, Value: "val1"},
{Name: xml.Name{Local: "attr2"}, Value: "val2"},
},
}
rawData, _ := xml.Marshal(v)
fmt.Println(string(rawData))
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question