du2986 2015-09-04 08:27
浏览 26
已采纳

在Golang中转换函数类型

// Each type have Error() string method.
// The error built-in interface type is the conventional interface for
// representing an error condition, with the nil value representing no error.
//  type error interface {
//   Error() string
//  }

func (f binFunc) Error() string {
    return "binFunc error"
}

func func_type_convert() {
    var err error
    err = binFunc(add)
    fmt.Println(err)
    fmt.Println(i)
}

I have two questions about the code above:

  • I don't know why the Error method executed, when add function was converted into binFunc type?
  • Why the add function converted result was able to assign to an err error interface variable?
  • 写回答

1条回答 默认 最新

  • duanmao1872 2015-09-04 11:00
    关注

    error is an interface:

    type error interface {
        Error() string
    }
    

    This means that any type which has a method: Error() string fulfills the interface and can be assigned to a variable of type error.

    binFunc has such a method:

    func (f binFunc) Error() string {
        return "binFunc error"
    }
    

    New developers in Go sometimes find this confusing because they don't realize it's possible to attach methods to more than just structs. In this case binFunc is defined liked this:

    type binFunc func(int, int) int
    

    So the way this works is you are allowed to convert any function which has the same signature: (from the spec)

    A function type denotes the set of all functions with the same parameter and result types.

    So if you create a function add:

    func add(x, y int) int {
        return x + y
    }
    

    You are allowed to convert this into a binFunc:

    binFunc(add)
    

    And because of the Error method on binFunc we defined above, we are then able to assign this new binFunc to a variable of type error:

    var err error
    var bf binFunc = binFunc(add)
    err = bf
    

    fmt.Println's behavior is to call .Error() on errors for you:

    1. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

    So to answer your questions:

    • The Error method is executed because fmt.Println looks for arguments of type error, invokes .Error() and prints the resulting string.
    • You are allowed to assign binFuncs to err because binFunc has an Error method. You cannot assign add directly to err because it does not have an Error method. But you are allowed to convert add to a binFunc because they have the same function signature, and by doing so you can then assign it to the err variable.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题