FAQ
Hello,

how to make this program work http://play.golang.org/p/HvJ1dqXF3z

I am generating code, where I need to keep the binary data
(json.RawMessage) in struct field so I can serve it as json without
encoding. I am not sure how to format the binary data (output
of json.Marshal(v) ) so that it can be written and retrieved from go
sourcefile. go/format gives error as the byte array is not formatted
correctly. If I remove the formatting, the output looks like

  package test

  // THIS FILE WAS AUTO-GENERATED

  var tmp = map[string]GenData{

{ "Reds" : GenData{Json: []byte{[123 34 78 97 109 101 34 58 34 82 101 100 115 34 44 34 67 111 108 111 114 115 34 58 91 34 67 114 105 109 115 111 110 34 44 34 82 101 100 34 44 34 82 117 98 121 34 44 34 77 97 114 111 111 110 34 93 125]}}},

{ "Blues" : GenData{Json: []byte{[123 34 78 97 109 101 34 58 34 66 108 117 101 115 34 44 34 67 111 108 111 114 115 34 58 91 34 67 114 105 109 115 111 110 34 44 34 82 101 100 34 44 34 82 117 98 121 34 44 34 77 97 114 111 111 110 34 93 125]}}},

}


this ofcourse didn't write the []byte as an array. Is there an even easier
way :-)

Code:
===

package main


import (

"bytes"

"encoding/json"

"fmt"

"go/format"

"os"

)


type ColorGroup struct {

Name string

Colors []string

}


type GenData struct {

Json *json.RawMessage

}


func writePackage(w *bytes.Buffer, pkg string) {

var pkgTmpl = `

package %s


// THIS FILE WAS AUTO-GENERATED

`

w.WriteString(fmt.Sprintf(pkgTmpl, pkg))

}


func main() {


cols := map[string]ColorGroup{

"Reds": ColorGroup{Name: "Reds", Colors: []string{"Crimson", "Red",
"Ruby", "Maroon"}},

"Blues": ColorGroup{Name: "Blues", Colors: []string{"Crimson", "Red",
"Ruby", "Maroon"}},

}


var w = new(bytes.Buffer)

writePackage(w, "test")

w.WriteString("var tmp = map[string]GenData{\n")

for k, v := range cols {

b, err := json.Marshal(v)

if err != nil {

fmt.Println("error:", err)

}


fmt.Fprintf(w, "{ \"%s\" : GenData{Json: []byte{%v}}},\n", k, b)

}

w.WriteString("\n}")


pretty, err := format.Source(w.Bytes())

if err != nil {

panic(err)

}


os.Stdout.Write(pretty)


}


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Bsr at Nov 12, 2015 at 7:35 pm
    I guess it probably should have been []byte(`%s`)
    On Thursday, November 12, 2015 at 1:13:58 PM UTC-5, bsr wrote:

    Hello,

    how to make this program work http://play.golang.org/p/HvJ1dqXF3z

    I am generating code, where I need to keep the binary data
    (json.RawMessage) in struct field so I can serve it as json without
    encoding. I am not sure how to format the binary data (output
    of json.Marshal(v) ) so that it can be written and retrieved from go
    sourcefile. go/format gives error as the byte array is not formatted
    correctly. If I remove the formatting, the output looks like

    package test

    // THIS FILE WAS AUTO-GENERATED

    var tmp = map[string]GenData{

    { "Reds" : GenData{Json: []byte{[123 34 78 97 109 101 34 58 34 82 101 100 115 34 44 34 67 111 108 111 114 115 34 58 91 34 67 114 105 109 115 111 110 34 44 34 82 101 100 34 44 34 82 117 98 121 34 44 34 77 97 114 111 111 110 34 93 125]}}},

    { "Blues" : GenData{Json: []byte{[123 34 78 97 109 101 34 58 34 66 108 117 101 115 34 44 34 67 111 108 111 114 115 34 58 91 34 67 114 105 109 115 111 110 34 44 34 82 101 100 34 44 34 82 117 98 121 34 44 34 77 97 114 111 111 110 34 93 125]}}},

    }


    this ofcourse didn't write the []byte as an array. Is there an even
    easier way :-)

    Code:
    ===

    package main


    import (

    "bytes"

    "encoding/json"

    "fmt"

    "go/format"

    "os"

    )


    type ColorGroup struct {

    Name string

    Colors []string

    }


    type GenData struct {

    Json *json.RawMessage

    }


    func writePackage(w *bytes.Buffer, pkg string) {

    var pkgTmpl = `

    package %s


    // THIS FILE WAS AUTO-GENERATED

    `

    w.WriteString(fmt.Sprintf(pkgTmpl, pkg))

    }


    func main() {


    cols := map[string]ColorGroup{

    "Reds": ColorGroup{Name: "Reds", Colors: []string{"Crimson", "Red",
    "Ruby", "Maroon"}},

    "Blues": ColorGroup{Name: "Blues", Colors: []string{"Crimson", "Red",
    "Ruby", "Maroon"}},

    }


    var w = new(bytes.Buffer)

    writePackage(w, "test")

    w.WriteString("var tmp = map[string]GenData{\n")

    for k, v := range cols {

    b, err := json.Marshal(v)

    if err != nil {

    fmt.Println("error:", err)

    }


    fmt.Fprintf(w, "{ \"%s\" : GenData{Json: []byte{%v}}},\n", k, b)

    }

    w.WriteString("\n}")


    pretty, err := format.Source(w.Bytes())

    if err != nil {

    panic(err)

    }


    os.Stdout.Write(pretty)


    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tamás Gulácsi at Nov 12, 2015 at 8:49 pm
    Can't use github.com/jteeuwen/go-bindata ?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Bsr at Nov 13, 2015 at 2:22 am
    thanks Tamas for pointing out. That looks a good fit.
    On Thursday, November 12, 2015 at 3:49:10 PM UTC-5, Tamás Gulácsi wrote:

    Can't use github.com/jteeuwen/go-bindata ?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 12, '15 at 6:14p
activeNov 13, '15 at 2:22a
posts4
users2
websitegolang.org

2 users in discussion

Bsr: 3 posts Tamás Gulácsi: 1 post

People

Translate

site design / logo © 2021 Grokbase