xml

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decoder added in v0.2.0

func Decoder[T any](b []byte, v *T) error

Decoder decodes XML bytes into T.

func Encoder added in v0.2.0

func Encoder[T any](v T) ([]byte, error)

Encoder encodes T to XML bytes.

func NewCodec

func NewCodec[T any]() encoding.Codec[T, []byte]

NewCodec returns an XML codec for T. It is safe for concurrent use.

Example
package main

import (
	"fmt"

	"github.com/foomo/goencode/xml"
)

func main() {
	type Data struct {
		XMLName struct{} `xml:"data"`
		Name    string   `xml:"name"`
	}

	c := xml.NewCodec[Data]()

	encoded, err := c.Encode(Data{Name: "example-123"})
	if err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	fmt.Printf("Encoded: %s\n", string(encoded))
}
Output:
Encoded: <data><name>example-123</name></data>

func NewStreamCodec

func NewStreamCodec[T any]() encoding.StreamCodec[T]

NewStreamCodec returns an XML stream codec for T. It is safe for concurrent use.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/foomo/goencode/xml"
)

func main() {
	type Data struct {
		XMLName struct{} `xml:"data"`
		Name    string   `xml:"name"`
	}

	c := xml.NewStreamCodec[Data]()

	var buf bytes.Buffer
	if err := c.Encode(&buf, Data{Name: "example-123"}); err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	var decoded Data
	if err := c.Decode(&buf, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded Name: %s\n", decoded.Name)
}
Output:
Decoded Name: example-123

func StreamDecoder added in v0.2.0

func StreamDecoder[T any](r io.Reader, v *T) error

StreamDecoder decodes T from an XML stream.

func StreamEncoder added in v0.2.0

func StreamEncoder[T any](w io.Writer, v T) error

StreamEncoder encodes T to an XML stream.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL