Skip to content

Latest commit

 

History

History
79 lines (64 loc) · 2.54 KB

README.md

File metadata and controls

79 lines (64 loc) · 2.54 KB

GoDoc codecov Build Status Go Report Card License Mentioned in Awesome Go

encoding

Package provides a generic interface to encoders and decoders

Example

package main
  
import (
        "flag"
        "fmt"
        "log"
        "strings"

        "github.com/mickep76/encoding"
        _ "github.com/mickep76/encoding/json"
        _ "github.com/mickep76/encoding/toml"
        _ "github.com/mickep76/encoding/yaml"
)

type Message struct {
        Name, Text string
}

type Messages struct {
        Messages []*Message
}

func main() {
        codec := flag.String("codec", "json", fmt.Sprintf("Codecs: [%s].", strings.Join(encoding.Codecs(), ", ")))
        indent := flag.String("indent", "", "Indent encoding (only supported by JSON codec)")
        flag.Parse()

        in := Messages{
                Messages: []*Message{
                        &Message{Name: "Ed", Text: "Knock knock."},
                        &Message{Name: "Sam", Text: "Who's there?"},
                        &Message{Name: "Ed", Text: "Go fmt."},
                        &Message{Name: "Sam", Text: "Go fmt who?"},
                        &Message{Name: "Ed", Text: "Go fmt yourself!"},
                },
        }

        var opts []encoding.Option
        if *indent != "" {
                opts = append(opts, encoding.WithIndent(*indent))
        }
        c, err := encoding.NewCodec(*codec, opts...)
        if err != nil {
                log.Fatal(err)
        }

        b, err := c.Encode(in)
        if err != nil {
                log.Fatal(err)
        }

        fmt.Printf("Codec: %s\n", *codec)
        fmt.Printf("Encoded:\n%s\n", string(b))

        out := Messages{}
        if err := c.Decode(b, &out); err != nil {
                log.Fatal(err)
        }

        fmt.Println("Decoded:")
        for _, m := range out.Messages {
                fmt.Printf("%s: %s\n", m.Name, m.Text)
        }
}