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) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| v | any | Value to encode or pointer to the destination for decoding. |
| start | StartElement | Start 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())}