Standard library / encoding/xml

encoding/xml.Encoder.EncodeToken()

Writes one XML token. It checks matching end tags but does not flush automatically. Expected stdout: '<a>Go</a>\n'.

Formal Syntax & Structure

go syntaxgo
func (enc *Encoder) EncodeToken(t Token) error

Parameters & Attributes

NameTypeDescription
tTokenXML token or token reader, according to the signature.

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)  s := xml.StartElement{Name: xml.Name{Local: "a"}}  must(enc.EncodeToken(s))  must(enc.EncodeToken(xml.CharData("Go")))  must(enc.EncodeToken(s.End()))  must(enc.Close())  fmt.Println(out.String())}
Go Reference