This is code :
import (
"bytes"
"encoding/xml"
"fmt")
func main() {
type body struct {
Message string `xml:"message"`
}
myXml := body{
Message:"This is <myText>",
}
w := &bytes.Buffer{}
enc := xml.NewEncoder(w)
enc.Indent("", " ")
if err := enc.Encode(myXml); err != nil {
panic(err)
}
request := w.String()
fmt.Println(request)
}
Is there any way that the value of field message do not encode.I want not encode. This is result :
<body>
<message>This is <myText></message>
</body>
Mission of encoding/xml
is to produce valid XML documents. Escaping the <
and >
characters is a must for a valid XML. Don't worry, the content of <message>
will be This is <myText>
, but this text must appear as This is <myText>
in the source of XML.
Note that using the xml:",innerxml"
tag value you could force outputting raw XML data as documented at xml.Marshal()
:
- a field with tag ",innerxml" is written verbatim, not subject to the usual marshaling procedure.
Like this:
type rawxml struct {
Data string `xml:",innerxml"`
}
type body struct {
Message rawxml `xml:"message"`
}
myXml := body{
Message: rawxml{"This is <myText>"},
}
This would output (try it on the Go Playground):
<body>
<message>This is <myText></message>
</body>
Or implementing and using custom xml.Marshaler
, but again, this is invalid XML, this is not what you want. What you have right now is exactly what you want.