如何转换具有相同签名的函数类型? [英] How can I convert function types with the same signature?

查看:67
本文介绍了如何转换具有相同签名的函数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在包装中,我有这个:

package pkg1

type SomeFuncType func (a interface{}, b interface{}) int

func PkgApiCall (x SomeFuncType) {
   ...
}

在使用此程序包的代码中,我有一些非常相似的内容:

In my code using this package, I have some very similar:

type MyFuncType func (a interface{}, b interface{}) int

现在,我想调用 pkg1.PkgApiCall(),但使用 MyFuncType 变量作为参数:

Now I want to call pkg1.PkgApiCall(), but with a variable of MyFuncType as argument:

package mypackage

func doingSomeThing(x MyFuncType) {
  pkg1.PkgApiCall(x)
}

它不能编译.我得到了错误

It doesn't compile. I get the error

./src1.go:97:7: error: incompatible type in initialization (cannot use type mypackage.MyFuncType as type pkg1.SomeFuncType)

我该如何克服?这些函数类型定义的函数具有完全相同的签名.

How could I get over this? These function types define functions with exactly the same signature.

推荐答案

通常的类型转换既适用于函数类型,也适用于非函数类型:

The usual type conversions work for function types just as well as they work for non-function types:

pkg1.PkgApiCall(SomeFuncType(x))

这篇关于如何转换具有相同签名的函数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆