Default function arguments

4,144 views
Skip to first unread message

David

unread,
Nov 15, 2009, 5:46:55 PM11/15/09
to golang-nuts
From what I've seen, Go doesn't support default function arguments?
Maybe I'm wrong in their implementation...
(I know struct members don't support default values, but I wasn't sure
if functions do).

// the following code doesn't work -- am I implementing something
wrong?
func something(a int, b int, c int = 5) int {
return a + b + c;
}

func main() {
resa := something(1,2,3);
resb := something(1,2);
fmt.Println(resa);
fmt.Println(resb);
}

Adam Langley

unread,
Nov 15, 2009, 5:48:38 PM11/15/09
to David, golang-nuts
On Sun, Nov 15, 2009 at 2:46 PM, David <david.t...@gmail.com> wrote:
> From what I've seen, Go doesn't support default function arguments?

Correct.



AGL

Wanton

unread,
Nov 18, 2009, 4:56:19 AM11/18/09
to golang-nuts
Is there a plan to add these optional parameters for go? I know you
won't be adding overloading so optional parameters would be really
helpful in some situations.
I use overloading in C# mostly for adding optional parameter values
(well until i can use C# 4.0 that supports optional parameters).

Ian Lance Taylor

unread,
Nov 18, 2009, 10:23:10 AM11/18/09
to Wanton, golang-nuts
There is no current plan for this, beyond the existing varargs
support, which I acknowledge is not the same thing.

Ian
Reply all
Reply to author
Forward
0 new messages