Does Go language have function / method overloading?

I am moving the C library to Go. The C function (with varargs) is defined as follows:

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

So, I created C shell 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 a function in Go as follows:

 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, is the function function (method) Go function overloaded, or does this error mean something else?

+91
go
Aug 08 2018-11-18T00:
source share
3 answers

No.

See the Go Language FAQ , in particular, the overload section.

Method dispatching is simplified if type matching is not required. Experience with other languages ​​has shown us that having different methods with the same name but different signatures is sometimes useful, but in practice it can also be confusing and fragile. Matching only by name and requiring type consistency was the main simplifying solution in the Go type system.

Update: 2016-04-07

Although Go still has no overloaded functions (and probably never will), the most useful overload function, namely calling a function with optional arguments and displaying default values ​​for missing ones, can be modeled using a function with variable numbers, which has since been added. But this happens when type checking is lost.

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

+115
Aug 08 '11 at 18:47
source share

Accordingly, this does not mean: http://golang.org/doc/go_for_cpp_programmers.html

The Conceptual Differences section says:

"Go does not support function overloading and does not support user-defined operators."

+14
Aug 08 '11 at 18:48
source share

func (e *Easy)SetOption(any []interface{})

The process converts the parameters to this empty interface{} .

The first type of transformation, and then the internal logical processes.

http://zerousm99.blogspot.kr/2015/01/golang-overload.html

+1
Jan 27 '15 at 6:29
source share



All Articles