Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for function overloading in Go?

Tags:

overloading

go

Is it possible to work similar way like the function overloading or optional parameter in C# using Golang? Or maybe an alternative way?

like image 652
Coder Avatar asked Oct 23 '12 10:10

Coder


People also ask

Is there function overloading in Go?

No it does not.

Why does Go not have overloading?

Why does Go not support overloading of methods and operators? Method dispatch is simplified if it doesn't need to do type matching as well.

Does Go support operator overloading and method overloading?

No, operator overloading is not a feature of Go.

Does rust have function overloading?

Rust allows for a limited form of operator overloading. There are certain operators that are able to be overloaded. To support a particular operator between types, there's a specific trait that you can implement, which then overloads the operator.


1 Answers

The idiomatic answer to optional parameters in Go is wrapper functions:

func do(a, b, c int) {
    // ...
}

func doSimply(a, b) {
    do(a, b, 42)
}

Function overloading was intentionally left out, because it makes code hard(er) to read.

like image 136
johnny-john Avatar answered Nov 18 '22 18:11

johnny-john