Logo

0x5a.live

for different kinds of informations and explorations.

GitHub - ake-persson/encoding: Go package provides a generic interface to encoders and decoders

Go package provides a generic interface to encoders and decoders - ake-persson/encoding

Visit SiteGitHub - ake-persson/encoding: Go package provides a generic interface to encoders and decoders

GitHub - ake-persson/encoding: Go package provides a generic interface to encoders and decoders

Go package provides a generic interface to encoders and decoders - ake-persson/encoding

Powered by 0x5a.live ๐Ÿ’—

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)
        }
}

GoLang Resources

are all listed below.

Resources

listed to get explored on!!

Made with โค๏ธ

to provide different kinds of informations and resources.