Giter Site home page Giter Site logo

orderedmap's Introduction

orderedmap

Orderedmap works like a map[string]interface{} but preserves the order in which keys have been added. It has been optimized performance-wise while still keeping memory usage at a minimum. All operations have O(1) time complexity.

Orderedmap is useful for generating human-readable JSON and can be easily marshalled using golang's encoding/json package.

Full example code

package main

import "fmt"
import "encoding/json"

import om "github.com/maurice2k/orderedmap"

func main() {
    status := om.NewOrderedMap(  // initializes a new odered map
        &om.KV{"http", om.NewOrderedMap(  // key-value with sub ordered map as value
            &om.KV{"code", 200},  // simple key-value
            &om.KV{"message", "OK"},
        )})

    response := om.NewOrderedMap().  // now with Setter
        Set("success", true).
        Set("status", status)

    contentResponse := om.NewOrderedMap(
        &om.KV{"user", om.NewOrderedMap(
            &om.KV{"id", 1337},
            &om.KV{"name", "John Doe"},
        )})

    contentResponse.Set("success", false)  // will be skipped in Append(..., false) call

    // append content to response without overwriting existing keys
    // (second parameter set to false)
    response.Append(contentResponse, false)

    jsonData, _ := json.MarshalIndent(response, "", "  ")
    fmt.Println(string(jsonData))
}

Output

{
  "success": true,
  "status": {
    "http": {
      "code": 200,
      "message": "OK"
    }
  },
  "user": {
    "id": 1337,
    "name": "John Doe"
  }
}

orderedmap's People

Contributors

maurice2k avatar nikogura avatar

Watchers

James Cloos 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.