Marshall
type Blockchain struct{
blocks []Block `json:"blocks"`
difficulty int `json:"difficulty"`
}
type Block struct{
index int `json:"index"`
timestamp string `json:"timestamp"`
data string `json:"data"`
previousHash string `json:"previousHash"`
hash string `json:"hash"`
nonce int `json:"nonce"`
}
When I try to inspect my structure with:
b, err := json.Marshal(blockchain)
if err != nil {
panic(err)
}
fmt.Println(string(b))
I get:
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
runtime stack:
runtime.throw(0x4e4094, 0xe)
Also, when initializing
var blockchain = new(Blockchain)
func (blockchain *Blockchain) AddBlock(block Block) []Block {
block.previousHash = latestBlock().hash
blockchain.blocks = append(blockchain.blocks, mineBlock(blockchain.difficulty))
return blockchain.blocks
}
func latestBlock() Block{
return blockchain.blocks[len(blockchain.blocks)-1]
}
When I do:
var s = fmt.Sprintf("%#+v", *blockchain)
print(s)
I get:
main.Blockchain{blocks:[]main.Block{main.Block{index:1, timestamp:"2019-04-06 19:21:37", data:"Genesis block", previousHash:"", hash:"54baff26aa7411352c7879c4ad4bdb86cae07d667d1ed1962225bf0f464f78a0", nonce:0}, main.Block{index:2, timestamp:"2019-04-06 19:21:37", data:"d.duck", previousHash:"54baff26aa7411352c7879c4ad4bdb86cae07d667d1ed1962225bf0f464f78a0", hash:"c4e5d38c907c5b5db77f651880121ebbcb75b1c26d476071d9d69e4ae70e6a11", nonce:1}, main.Block{index:3, timestamp:"2019-04-06 19:21:37", data:"dumbo", previousHash:"c4e5d38c907c5b5db77f651880121ebbcb75b1c26d476071d9d69e4ae70e6a11", hash:"12c3f3595ce28923204c87a6e244ec66c34a7efb13a3acd3b6b57cea75a629d8", nonce:2}, main.Block{index:4, timestamp:"2019-04-06 19:21:37", data:"clown", previousHash:"12c3f3595ce28923204c87a6e244ec66c34a7efb13a3acd3b6b57cea75a629d8", hash:"8907be04c22ed0bc1d7d7b7ce13cf86f11f6fb3261ee34aaafc1fe6a703320b5", nonce:3}, main.Block{index:5, timestamp:"2019-04-06 19:21:37", data:"cod", previousHash:"8907be04c22ed0bc1d7d7b7ce13cf86f11f6fb3261ee34aaafc1fe6a703320b5", hash:"2d514943c6d86940dfca66e720c2aeade7de1fa1a053d36f81cbb134c02d0f26", nonce:4}, main.Block{index:6, timestamp:"2019-04-06 19:21:37", data:"omaha, omaha", previousHash:"2d514943c6d86940dfca66e720c2aeade7de1fa1a053d36f81cbb134c02d0f26", hash:"ef630c9a270a0ac52a2f5a0e2821ea91807b2471e4e4f7204c2edb4f44dd231b", nonce:5}, main.Block{index:7, timestamp:"2019-04-06 19:21:37", data:"double", previousHash:"ef630c9a270a0ac52a2f5a0e2821ea91807b2471e4e4f7204c2edb4f44dd231b", hash:"0c72339be0b54f7e87fcf48adf13f1d3b0e00ba56660906e8283758a74591ca3", nonce:6}, main.Block{index:8, timestamp:"2019-04-06 19:21:37", data:"fake", previousHash:"0c72339be0b54f7e87fcf48adf13f1d3b0e00ba56660906e8283758a74591ca3", hash:"ca31e0372579f0bfb00ac2ddb2127de0dff4d500f7dd6c507db7439380a40862", nonce:7}, main.Block{index:9, timestamp:"2019-04-06 19:21:37", data:"reverse", previousHash:"ca31e0372579f0bfb00ac2ddb2127de0dff4d500f7dd6c507db7439380a40862", hash:"ce4a1386c3aa9e45d1a5d5d3cc5a7d9d0b4e0e2d153a7c9518369a3ee65d5368", nonce:8}}, difficulty:4}
The keys and fields are visible but the keys are not enclosed in double quotes like the values are.
Updated the structs to have exportable fields.
type Blockchain struct{
Blocks []Block `json:"blocks"`
Difficulty int `json:"difficulty"`
}
type Block struct{
Index int `json:"index"`
Timestamp string `json:"timestamp"`
Data string `json:"data"`
PreviousHash string `json:"previousHash"`
Hash string `json:"hash"`
Nonce int `json:"nonce"`
}
When trying the same Marshal again I get the same error.