Go, quicktype!
quicktype
can now output Go in addition to C#.
Example: Latest Bitcoin block
For example, here's the JSON for the latest Bitcoin block from blockchain.info/latestblock:
{
"hash": "00000000000000000039812e769f24205e041b453c293e27b62589875b4eb446",
"time": 1500137916,
"block_index": 1598634,
"height": 475937,
"txIndexes": [
268140937,
268134345,
268134652
]
}
Generate typed Go code
Using the quicktype
command (available via npm i -g quicktype
), we can generate strongly typed Go code for working with this Bitcoin data:
$ quicktype -o latest_block.go https://blockchain.info/latestblock
This creates the file latest_block.go
with the following Go code:
// To parse and unparse this JSON data, add this code to your project and do:
//
// r, err := UnmarshalBitcoinBlock(bytes)
// bytes, err = r.Marshal()
package main
import "encoding/json"
type BitcoinBlock OtherBitcoinBlock
func UnmarshalBitcoinBlock(data []byte) (BitcoinBlock, error) {
var r BitcoinBlock
err := json.Unmarshal(data, &r)
return r, err
}
func (r *BitcoinBlock) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type OtherBitcoinBlock struct {
BlockIndex int64 `json:"block_index"`
Hash string `json:"hash"`
Height int64 `json:"height"`
Time int64 `json:"time"`
TxIndexes []int64 `json:"txIndexes"`
}
Play with it
Visit go.quicktype.io to try it out. We eagerly await your feedback!