Go语言是否有函数/方法重载? [英] Does the Go language have function/method overloading?

查看:2399
本文介绍了Go语言是否有函数/方法重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个C库移植到Go上。一个C函数(带有可变参数)定义如下:

  curl_easy_setopt(CURL * curl,CURLoption option,...); 

所以我创建了包装C函数:

  curl_wrapper_easy_setopt_str(CURL * curl,CURL选项选项,char *参数); 
curl_wrapper_easy_setopt_long(CURL * curl,CURL选项,长参数);

如果我在Go中定义函数,如下所示:

  func(e * Easy)SetOption(option Option,param string){
e.code = Code(C.curl_wrapper_easy_setopt_str(e.curl,C.CURLoption(option ),C.CString(param)))
}

func(e * Easy)SetOption(option option,param long){
e.code = Code(C。 curl_wrapper_easy_setopt_long(e.curl,C.CURLoption(option),C.long(param)))
}

Go编译器抱怨:

  *简单·SetOption在这个块中重新声明

Go支持函数(方法)重载,或者这个错误是否意味着其他的东西?

解决方案

请注意 =http://golang.org/doc/go_faq.html =noreferrer> Go Language FAQ ,特别是重载


如果不需要方法调度做类型匹配。使用其他语言的经验告诉我们,使用同一名称但签名不同的多种方法偶尔有用,但在实践中也可能会令人困惑和脆弱。只与名称匹配并要求类型一致性是Go类型系统中一个重要的简化决策。



关于运算符重载,它似乎比绝对需求更为方便。再次,没有它,事情就更简单了。


更新:2016-04-07



虽然Go仍然没有重载函数(并且可能永远不会),但是使用可变参数函数可以模拟重载的最有用的特性,即使用可选参数调用函数并推断缺省值的默认值,已被添加。但是这是在类型检查丢失的情况下。



例如: http://changelog.ca/log/2015/01/30/golang


I'm porting a C library to Go. A C function (with varargs) is defined like this:

curl_easy_setopt(CURL *curl, CURLoption option, ...); 

So I created wrapper C functions:

curl_wrapper_easy_setopt_str(CURL *curl, CURLoption option, char* param);
curl_wrapper_easy_setopt_long(CURL *curl, CURLoption option, long param);

If I define function in Go like this:

func (e *Easy)SetOption(option Option, param string) {
    e.code = Code(C.curl_wrapper_easy_setopt_str(e.curl, C.CURLoption(option), C.CString(param)))
}

func (e *Easy)SetOption(option Option, param long) {
    e.code = Code(C.curl_wrapper_easy_setopt_long(e.curl, C.CURLoption(option), C.long(param)))
}

The Go compiler complains:

*Easy·SetOption redeclared in this block

So does Go support function (method) overloading, or does this error mean something else?

解决方案

No it does not.

See the Go Language FAQ, and specifically the section on overloading.

Method dispatch is simplified if it doesn't need to do type matching as well. Experience with other languages told us that having a variety of methods with the same name but different signatures was occasionally useful but that it could also be confusing and fragile in practice. Matching only by name and requiring consistency in the types was a major simplifying decision in Go's type system.

Regarding operator overloading, it seems more a convenience than an absolute requirement. Again, things are simpler without it.

Update: 2016-04-07

While Go still does not have overloaded functions (and probably never will), the most useful feature of overloading, that of calling a function with optional arguments and inferring defaults for those omitted can be simulated using a variadic function, which has since been added. But this comes at the loss of type checking.

For example: http://changelog.ca/log/2015/01/30/golang

这篇关于Go语言是否有函数/方法重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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