Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print function that consumes json.RawMessage directly

Tags:

json

go

I'm doing some work with Elasticsearch and the query return a Source object which it's type is *json.RawMessage.

I only want to print it to screen without creating struct model for it and doing the obvious json.Marshal.

Is there a print function that will consume *json.RawMessage type and print it to screen?

Code sample:

 for _, hit := range serachResult.Hits.Hits {
    fmt.Println(hit.Source, "\n")
 }

This code run will result in un-readable array of bytes, apparently without the ability to just build a string from the raw message.

like image 522
Shikloshi Avatar asked Jun 06 '16 13:06

Shikloshi


1 Answers

You can use %s to printf:

for _, hit := range serachResult.Hits.Hits {
   fmt.Printf("%s\n", hit.Source)
}
like image 110
OneOfOne Avatar answered Oct 31 '22 20:10

OneOfOne