duanrenchuo9244 2016-03-14 21:04
浏览 53
已采纳

我想了解为什么要创建一种类型来处理Go中的错误,以及如何确定它应具有的底层类型

I'm working through A Tour of Go - Exercise: Errors. It holds my hand as I add error handling to a square root function.

Here is my solution:

package main

import (
    "fmt"
    "math"
)

type ErrNegativeSqrt float64

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

func Sqrt(x float64) (float64, error) {
    z := 1.0
    margin := 0.00000000000001
    for {
        if x < 0 {
            return 0, ErrNegativeSqrt(x)
        }
        previousZ := z
        z = z - (z*z-x)/(2*z)
        if math.Abs(previousZ-z) < margin {
            fmt.Println(previousZ, "-", z, "=", previousZ-z)
            break
        }
    }
    fmt.Println("math.Sqrt:", math.Sqrt(x))
    return z, nil
}

func main() {
    fmt.Println(Sqrt(2))
    fmt.Println(Sqrt(-2))
}

I am having trouble understanding the line:

type ErrNegativeSqrt float64

I have two questions:

  1. Why is the underlying type of ErrNegativeSqrt "float64" and not "error"?

and

  1. Why create a type in the first place? Why do we create an error type and add a method to it instead of just having a free-standing function?

I'm a beginner at Go and I'd like to understand. Thank you so much.

  • 写回答

3条回答 默认 最新

  • dtz55359 2016-03-14 21:22
    关注

    I'm gonna answer your second question first: you add a method to your type so ErrNegativeSqrt implements the error interface, which means other code that does not know about the specifics of ErrNegativeSqrt can use it as any other error.

    As the error interface only requires a method Error() with return type string, a common type to use is just a string with the Error method returning that string (defined in package "errors").

    I don't know why it was decided to use float64 as the error type in this case, but the main advantage that I see is that if you had several functions that could return a similar error, you'd have to do the following:

    func Method1() error{
      ...
      return errors.New(fmt.Sprintf("cannot Sqrt negative number: %g", number)
    }
    
    func Method2() error{
      ...
      return errors.New(fmt.Sprintf("cannot Sqrt negative number: %g", number2)
    }
    

    And repeat for any function that needs to return a similar thing. With the code that you posted, you only declare the number responsible for the error when you create it, and the method "Error()" returns the formated string for all.

    func Method1() error{
      ...
      ErrNegativeSqrt(number)
    }
    
    func Method2() error{
      ...
      ErrNegativeSqrt(number)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100