Does the Go language have function/method overloading?

Go

Go Problem Overview


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?

Go Solutions


Solution 1 - Go

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.

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

Solution 2 - Go

According to this, it doesn't: http://golang.org/doc/go_for_cpp_programmers.html

In the Conceptual Differences section, it says:

>Go does not support function overloading and does not support user defined operators.

Solution 3 - Go

Even though this question is really old, what I still want to say is that there is a way to acheive something close to overloading functions. Although it may not make the code so easy to read.

Say if you want to overload the funtion Test():

func Test(a int) {
	println(a);
}
func Test(a int, b string) {
	println(a);
	println(b);
}

The code above will cause error. However if you redefine the first Test() to Test1() and the second to Test2(), and define a new function Test() using go's ..., you would be able to call the function Test() the way it is overloaded. code:

package main;

func Test1(a int) {
	println(a);
}
func Test2(a int, b string) {
	println(a);
	println(b);
}
func Test(a int, bs ...string) {
	if len(bs) == 0 {
		Test1(a);
	} else {
		Test2(a, bs[0]);
	}
}
func main() {
	Test(1);
	Test(1, "aaa");
}

output:

1
1
aaa

see more at: https://golangbyexample.com/function-method-overloading-golang/

Solution 4 - Go

Golang 1.18 now has function overloading: https://go.dev/doc/tutorial/generics

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarius KucinskasView Question on Stackoverflow
Solution 1 - GoLawrence DolView Answer on Stackoverflow
Solution 2 - GoMerigrimView Answer on Stackoverflow
Solution 3 - GoKevin LiuView Answer on Stackoverflow
Solution 4 - GoRulyView Answer on Stackoverflow