当我尝试 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))
我得到:
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
runtime stack:
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"`
}
当再次尝试相同的 Marshal 时,我得到了同样的错误。
最佳答案
程序用完了堆栈空间,因为 Blockchain.MarshalJSON 方法和 json.Marshal 函数递归地相互调用。
通过删除 Blockchain.MarshalJSON 方法进行修复。不需要该方法。
关于json - Golang 运行时 : goroutine stack exceeds 1000000000-byte limit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554434/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'
是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://
我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":