Golang运行时:goroutine堆栈超过了1000000000字节的限制

时间:2019-04-06 23:43:54

标签: json go marshalling

当我尝试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"`
}

当我尝试使用以下方法检查我的结构时:

b, err := json.Marshal(blockchain)
if err != nil {
    panic(err)
}
fmt.Println(string(b))

我得到:

  

运行时:goroutine堆栈超过1000000000字节限制

     

严重错误:堆栈溢出

     

运行时堆栈:

     

runtime.throw(0x4e4094,0xe)

此外,初始化时

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]
}

当我这样做时:

var s = fmt.Sprintf("%#+v", *blockchain)
print(s)

我得到:

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}

键和字段可见,但键没有像值一样用双引号引起来。

将结构更新为具有可导出的字段。

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"`
}

再次尝试使用相同的元帅时,我会遇到相同的错误。

1 个答案:

答案 0 :(得分:1)

该程序用完了堆栈空间,因为Blockchain.MarshalJSON方法和json.Marshal函数相互递归调用。

通过删除Blockchain.MarshalJSON方法进行修复。不需要该方法。

https://play.golang.org/p/vvXvRnRIpWL