Standard library / encoding/xml

encoding/xml.Encoder.EncodeElement()

Encodes a value under an explicit start element, including its attributes. Expected stdout: '<lang>Go</lang>\n'.

Formal Syntax & Structure

go syntaxgo
func (enc *Encoder) EncodeElement(v any, start StartElement) error

Parameters & Attributes

NameTypeDescription
vanyValue to encode or pointer to the destination for decoding.
startStartElementStart element specifying the XML name and attributes.

Return Value

error

Nil on success; otherwise the operation error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "encoding/xml"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var out bytes.Buffer  enc := xml.NewEncoder(&out)  must(enc.EncodeElement("Go", xml.StartElement{Name: xml.Name{Local: "lang"}}))  fmt.Println(out.String())}
Go Reference