如何在Go语言中连接字符串和float64?

huangapple go评论119阅读模式
英文:

How to concat a string and float64 on go?

问题

我正在尝试解决这个问题:http://tour.golang.org/#58

这是我所做的:

  1. #省略了导入
  2. type ErrNegativeSqrt float64
  3. func (e ErrNegativeSqrt) Error() string {
  4. return "不能对负数进行平方根运算:" + string(e)
  5. }
  6. func Sqrt(f float64) (float64, error) {
  7. if f < 0 {
  8. return 0, ErrNegativeSqrt(1)
  9. }
  10. # 在这里计算 z...
  11. return z, nil
  12. }
  13. #省略了主函数

我还尝试过 e.String()e.string(),但它们也没有起作用...

英文:

I am trying to solve this: http://tour.golang.org/#58

Here is what I have done;

  1. #imports omitted
  2. type ErrNegativeSqrt float64
  3. func (e ErrNegativeSqrt) Error() string {
  4. return &quot;Cannot Sqrt negative number: &quot; + string(e)
  5. }
  6. func Sqrt(f float64) (float64, error) {
  7. if f &lt; 0 {
  8. return 0, ErrNegativeSqrt(1)
  9. }
  10. # calculate z here...
  11. return z, nil
  12. }
  13. # main omitted

I have also tried e.String() and e.string() but those didn't work too...

答案1

得分: 9

尝试使用fmt

  1. import "fmt"
  2. ...
  3. return fmt.Sprint("Cannot Sqrt negative number ", float64(e))
英文:

Try using the fmt package

  1. import &quot;fmt&quot;
  2. ...
  3. return fmt.Sprint(&quot;Cannot Sqrt negative number &quot;, float64(e))

huangapple
  • 本文由 发表于 2012年9月1日 04:00:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/12221793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定