草庐IT

去旅行练习 : Errors: using Sprintf with %f to avoid infinite recursion

coder 2024-07-07 原文

我正在学习 Go 教程,在 Errors 练习中它提到在 Error 函数中调用 Sprint(f) 会导致一个问题,这是一个无限循环。此处解释了为什么会发生这种情况:Error, infinite loop

在我的第一个实现中,尽管我使用了带有 %f 动词的 Sprintf:

func (e ErrNegativeSqrt) Error() string {
    return fmt.Sprintf("cannot Sqrt negative number: %f", e)
}

这似乎避免了这个问题,我想知道这是否是因为 %f 动词需要一个 float ,所以它强制它将 e 视为一个 float ?这次旅行提到assignment requires explicit conversion ,但是我认为这不会影响这种情况?

还是我完全偏离了目标,这里发生了其他事情?

最佳答案

这种无限循环情况仅适用于 vsxX q.

参见此处:https://github.com/golang/go/blob/6f51082da77a1d4cafd5b7af0db69293943f4066/src/fmt/print.go#L615

关于去旅行练习 : Errors: using Sprintf with %f to avoid infinite recursion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43450813/

有关去旅行练习 : Errors: using Sprintf with %f to avoid infinite recursion的更多相关文章

  1. 牛客网专项练习30天Pytnon篇第02天 - 2

    1.在Python3中,下列关于数学运算结果正确的是:(B)a=10b=3print(a//b)print(a%b)print(a/b)A.3,3,3.3333...B.3,1,3.3333...C.3.3333...,3.3333...,3D.3.3333...,1,3.3333...解析:    在Python中,//表示地板除(向下取整),%表示取余,/表示除(Python2向下取整返回3)2.如下程序Python2会打印多少个数:(D)k=1000whilek>1:    print(k)k=k/2A.1000 B.10C.11D.9解析:    按照题意每次循环K/2,直到K值小于等

  2. ruby-on-rails - RSpec 未定义方法 "errors_on" - 2

    我想使用RSpec测试以下模型:classLanguagelanguage_spec.rbdescribeLanguagedodescribe'titlevalidation'docontext'titleispresent'dobefore(:each)do@lang=Language.new(title:'English')endit'isvalidwithpresenttitle'doexpect(@lang).tohave_exactly(0).errors_on(:title)endendcontext'titleisnotpresent'dobefore(:each)do@

  3. ruby-on-rails - Rails action Mailer raise_delivery_errors,它是如何工作的?如何检测反弹? - 2

    我有一个发送大量邮件的Rails4.2.0应用程序,它是一个在线学习平台。目前我遇到退回问题,很多邮件因为邮件地址无效而退回。一种方法是手动解决问题,开始从数据库中删除它们。但这不合适,因为大约有10000个用户注册。现在我的问题是什么config.action_mailer.raise_delivery_errors=true是吗?它有什么作用?我如何从中得到回应?mail()方法是否有一个返回值,我可以在其中查看邮件是否已发送?是否有方法或最佳实践来检测邮件是否已送达? 最佳答案 如果设置为false,邮件将静默捕获并忽略通过尝

  4. ruby-on-rails - Mongoid::Errors::DocumentNotFound raise_not_found_error - 2

    *在底部更新*当寻找不存在的用户时,我得到:Mongoid::Errors::DocumentNotFoundinUsersController#showProblem:Document(s)notfoundforclassUserwithid(s)22.Summary:WhencallingUser.findwithanidorarrayofids,eachparametermustmatchadocumentinthedatabaseorthiserrorwillberaised.Thesearchwasfortheid(s):22...(1total)andthefollowin

  5. ruby-on-rails - 如何在 Rails 中更改 "3 errors prohibited this foobar from being saved"验证消息? - 2

    在我的Rails应用程序中,我在我的事件记录对象中使用了验证助手,它们非常棒。当出现问题时,我会在我的网页上看到标准的“3个错误禁止保存此foobar”以及个别问题。有什么方法可以用我自己的方式覆盖这个默认消息吗? 最佳答案 用于显示错误的error_messages_for助手接受一个:header_message选项,该选项允许您更改默认标题文本。如:error_messages_for'model',:header_message=>"Youhavesomeerrorsthatpreventedsavingthismodel"

  6. ruby-on-rails - 解决 ruby​​ 中的旅行商问题(50 多个位置) - 2

    我在一家express公司工作。我们目前通过“手动”解决了50多个位置路线。我一直在考虑使用GoogleMapsAPI来解决这个问题,但我读到有24分的限制。目前我们在服务器中使用Rails,所以我正在考虑使用ruby​​脚本来获取50多个位置的坐标并输出合理的解决方案。您会使用什么算法来解决这个问题?Ruby是解决这类问题的好编程语言吗?你知道任何现有的ruby​​脚本吗? 最佳答案 这可能是您正在寻找的:警告:此站点被firefox标记为攻击站点-但我似乎没有。其实我之前用过没问题[检查URL的修订历史]rubyquiz似乎已关

  7. ruby-on-rails - gem "better_errors"不工作 - Rails 3.2 - 2

    我想在我的Rails3.2应用程序中使用gem“better_errors”,但它仍然是显示的默认错误页面。在我的Gemfile中:group:developmentdogem'quiet_assets','>=1.0.1'gem'better_errors','>=0.3.2'gem'binding_of_caller','>=0.6.8'end我做了一个“捆绑安装”然后bundleshowbetter_errors/usr/local/lib/ruby/gems/1.9.1/gems/better_errors-0.5.0一切似乎都已配置好。我在开发环境中运行我的Rails服务器r

  8. ruby-on-rails - Rails Heroku 服务器回形针 Amazon S3 - AWS::S3::Errors::RequestTimeout - 2

    当我用附加图像保存我的konkurrencer模型时,我在Heroku上收到错误,但在我的本地服务器上没有。.我的heroku日志:←[36m2012-04-01T17:45:37+00:00app[web.1]:←[0mStartedPOST"/admin/konkurrencers"forMYIPat2012-04-0110:45:37-0700←[33m2012-04-01T17:46:01+00:00heroku[router]:←[0mPOSTwww.konkurrencerher.dk/admin/konkurrencersdyno=web.1queue=0wait=0ms

  9. ruby - better_errors 要求 Ruby 版本 >= 2.0.0 - 2

    Bundle无法安装better_errors。Gem::InstallError:better_errorsrequiresRubyversion>=2.0.0.Anerroroccurredwhileinstallingbetter_errors(2.0.0),andBundlercannotcontinue.Makesurethat`geminstallbetter_errors-v'2.0.0'`succeedsbeforebundling.但是geminstallbetter_errors工作正常。ruby--version表示ruby2.0.0p353(2013-11-22

  10. ruby-on-rails - ActiveRecord::Errors 的 Rails 3 替代品是什么? - 2

    ActiveRecord::Errors的Rails3替代品是什么?在Rails2.3.8中,这是一个对象:>>ActiveRecord::Errors=>ActiveRecord::Errors在Rails3.0.0rc中,你会得到一个NameError:>>ActiveRecord::ErrorsNameError:uninitializedconstantActiveRecord::Errorsfrom(irb):2我正在尝试制作wizardly生成器与Rails3一起工作。$railsgwizardly_scaffoldhome但失败了:/Library/Ruby/Gems/1

随机推荐