Skip to content

Instantly share code, notes, and snippets.

@eduncan911
Created March 1, 2016 22:25
Show Gist options
  • Save eduncan911/c51a3cd6072ea6b39ace to your computer and use it in GitHub Desktop.
Save eduncan911/c51a3cd6072ea6b39ace to your computer and use it in GitHub Desktop.

Revisions

  1. eduncan911 created this gist Mar 1, 2016.
    50 changes: 50 additions & 0 deletions json_indent.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    /*Package main is a simple test of Json Identing (aka pretty print)
    To run:
    $ go run main.go
    {
    "eventId": "xyz",
    "articleId": "123"
    }
    The json is formatted nicely with linebreaks and 2x spaces.
    */
    package main

    import (
    "encoding/json"
    "fmt"
    )

    type ArticleDrafted struct {
    *Event
    }

    type Event struct {
    EventID EventID `json:"eventId,omitempty"`
    ArticleID ArticleID `json:"articleId,omitempty"`
    }

    type EventID string
    type ArticleID string

    func main() {

    t := ArticleDrafted{
    Event: &Event{
    EventID: "xyz",
    ArticleID: "123",
    },
    }

    b, err := json.MarshalIndent(&t, "", " ")
    if err != nil {
    fmt.Println(err)
    return
    }

    fmt.Println(string(b))

    }