Coder Social home page Coder Social logo

macode's Introduction

macode

macode is a Go package that provides functionality to encode (flatten) and decode (reconstruct) maps into ordered pairs of slices. It uses Go 1.18 generics, making it versatile and compatible with a wide range of types.

The macode package is particularly useful when you need to convert maps into a flat structure, for example, for serialization or for compatibility with certain data formats. Moreover, it ensures idempotent conversions, which is crucial for reproducibility of results in many applications.

Installation

To install the macode package, you can use go get:

go get github.com/WhiteRaven777/macode

Usage

Here is a basic example of how to use the macode package:

package main

import (
	"fmt"
	"os"
	"reflect"

	"github.com/WhiteRaven777/macode"
)

func main() {
	m := map[int]string{
		3: "three",
		1: "one",
		2: "two",
	}

	p := macode.New(m)
	enc := p.Encode(m)
	fmt.Println(enc) // Output: [{1 one} {2 two} {3 three}]

	dec := p.Decode(enc)
	fmt.Println(dec) // Output: map[1:one 2:two 3:three]

	// Check that the original map and the decoded map are deeply equal
	if !reflect.DeepEqual(m, dec) {
		fmt.Println("The original map and the decoded map are not equal")
		os.Exit(1)
	} else {
		fmt.Println("The original map and the decoded map are equal")
	}
}

In this example, a map is encoded into an ordered slice of pairs using the Encode method, and then it is decoded back into a map using the Decode method. The original map and the decoded map are then compared using reflect.DeepEqual to ensure they are deeply equal.

Testing

You can run the tests for the macode package using go test:

go test github.com/WhiteRaven777/macode

License

This project is licensed under the terms of the MIT license.

macode's People

Contributors

whiteraven777 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.