Golang encoding/xml.CharData.Copy() function examples

package encoding/xml

Golang encoding/xml.CharData.Copy() function usage examples

Example 1 :

 func (dec *decoder) readCharData() ([]byte, error) {
 var tok xml.Token
 var err error

 if tok, err = dec.Token(); err != nil {
 return nil, err
 }

 if t, ok := tok.(xml.CharData); ok {
 return []byte(t.Copy()), nil  /// <--- here
 } else {
  return nil, invalidXmlError
 }
 }

Example 2 :

 func CopyToken(t Token) Token {
  switch v := t.(type) {
  case CharData:
 return v.Copy()
  case Comment:
 return v.Copy()
  case Directive:
 return v.Copy()
  case ProcInst:
 return v.Copy()
  case StartElement:
 return v.Copy()
  }
  return t
 }

Reference :

http://golang.org/pkg/encoding/xml/#CharData.Copy

Advertisement