tags:

views:

173

answers:

3

Can Go have optional parameters? Or can I just define two functions with the same name and a different number of arguments?

+4  A: 

Go does not have optional parameters nor does it support method 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.

Andrew Hare
Thanks. I just gave my functions different names.
devyn
+2  A: 

No -- neither. Per the Go for C++ programmers docs,

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

I can't find an equally clear statement that optional parameters are unsupported, but they are not supported either.

Alex Martelli
"There is no current plan for this [optional parameters]." Ian Lance Taylor, Go language team.http://groups.google.com/group/golang-nuts/msg/030e63e7e681fd3e
peterSO
+1  A: 

Neither optional parameters nor function overloading are supported in Go. Go does support a variable number of parameters: Passing arguments to ... parameters

peterSO