Does function or method support argument default value?

39,183 views
Skip to first unread message

MC.Spring

unread,
Feb 8, 2010, 10:15:00 AM2/8/10
to golang-nuts
Hi, guys

I just realize that go does not support function or method argument
default value?! Or I have missed some thing around?

Though, we can use closure for some purposes.But if there is a default
implamention it's will be much suitable.

Jason Catena

unread,
Feb 8, 2010, 10:35:00 AM2/8/10
to golang-nuts
Why does this need additional, special support in the language? Go
has ... to support a variable number of arguments, from which you
could figure out which type of call it means, and supply defaults for
missing arguments. Or you could pass some out-of-range or zero value
like nil or -1 or even 0 to required arguments to ask for a default
value. This has a long tradition in C, so why are function-specific
calling conventions not enough? I think I'd rather see myfunc(nil,
nil, nil, nil, nil) than myfunc(, , , ,) to say "do what you do with
all default values", since while I'm developing a program it conflates
whether I missed a parameter completely or meant to ask for a default
value. This can cause silent errors in cases where the default value
is no acceptable to the overall program. (As the code base grows we
won't always be able to control the defaults for code we use in
others' packages.) I also don't see why it's necessary to add a
special token to represent "default value", since we can accomplish
this without adding more symbols to the language.

Michael Hoisie

unread,
Feb 8, 2010, 12:31:22 PM2/8/10
to golang-nuts
Go doesn't have default parameter values. In my opinion it would be a
valuable feature.

Jason, just because there are ways to hack around it, and it's not
available in C, doesn't mean it shouldn't be considered for Go. Most
modern languages have a concept of default parameters, and they're
widely used in these languages.

In the Go library there are tons of situations where this feature
would be useful. I can think of two examples off the top of my head:

1. strings.Split requires a third parameter n, which is the most
number of substrings to return. The most common case is 0, so the
method could be written as:
func Split(s, sep string, n int = 0) []string

2. The template package has two methods: Parse and MustParse. Both
have the exact same signature, but one panics when there's an error,
so these methods could be reduced to one:

func Parse(s string, fmap FormatterMap, panicOnError bool=false) (t
*Template, err os.Error) .

In both cases, the interface is reduced to a more simple form.

- Mike

Peter Bourgon

unread,
Feb 8, 2010, 1:54:12 PM2/8/10
to golang-nuts
Default parameters introduce a variability and unpredictability that
Google has deemed Bad™:

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Default_Arguments

Go is in many ways the language manifestation of Google's [C++] style
guide. While all points in there are arguable I find this one pretty
reasonable. So, I doubt this 'feature' will (should) be considered.

Michael Hoisie

unread,
Feb 8, 2010, 2:17:22 PM2/8/10
to golang-nuts
.. and yet it's okay in Google's python style guidelines:

http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Default_Argument_Values

There are clear pros and cons to this issue, but I'd rather look at
the tradeoffs and usability benefits than simply using a strawman and
comparing it to Google's C++ style guidelines.

- Mike

On Feb 8, 10:54 am, Peter Bourgon <peterbour...@gmail.com> wrote:
> Default parameters introduce a variability and unpredictability that
> Google has deemed Bad™:
>

> http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Defaul...

Peter Bourgon

unread,
Feb 8, 2010, 2:38:03 PM2/8/10
to golang-nuts
> .. and yet it's okay in Google's python style guidelines:
>
> http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Default_Argument_Values

Huh. Learn something new every day.

> There are clear pros and cons to this issue, but I'd rather look at
> the tradeoffs and usability benefits than simply using a strawman and
> comparing it to Google's C++ style guidelines.

I don't know if it's a strawman, but point taken.

Reply all
Reply to author
Forward
0 new messages