xml

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec[T any] struct{}

Codec is a Codec[T] backed by encoding/xml. 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 NewCodec

func NewCodec[T any]() *Codec[T]

NewCodec returns an XML serializer for T.

func (Codec[T]) Decode

func (Codec[T]) Decode(b []byte, v *T) error

func (Codec[T]) Encode

func (Codec[T]) Encode(v T) ([]byte, error)

type StreamCodec

type StreamCodec[T any] struct{}

StreamCodec is a StreamCodec[T] backed by encoding/xml. 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 NewStreamCodec

func NewStreamCodec[T any]() *StreamCodec[T]

NewStreamCodec returns an XML stream serializer for T.

func (StreamCodec[T]) Decode

func (StreamCodec[T]) Decode(r io.Reader, v *T) error

func (StreamCodec[T]) Encode

func (StreamCodec[T]) Encode(w io.Writer, v T) error

Jump to

Keyboard shortcuts

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