Go: proposal: Go 2: unified return parameter format

Created on 20 Mar 2019  路  6Comments  路  Source: golang/go

Problem
when we return a value,now is:

func A() string{}
func B() (string){}
func C() (string,string){}
func D() (s string){}
func E() (s1 string,s2 string){}

some need brackets, and some not

can Go 2 unified all return parameter format in the same?

all has no brackets

func A() string{}
func B() string,string{}
func C() s string{}
func D() s1 string,s2 string{}

or all has brackets

func A() (string){}
func B() (string, string){}
func C() (s string){}
func D() (s1 string, s2 string){}

i think it will make the code looks more cleaner.

FrozenDueToAge Go2 LanguageChange Proposal

Most helpful comment

I don't think Go should enforce that you always need to use parenthesis around the return arguments.
But there's also nothing to prevent you from using that style yourself. Your example:

func A() (string){}
func B() (string, string){}
func C() (s string){}
func D() (s1 string, s2 string){}

Is already Valid go code, even though you could optionally drop the parens for single-value (non-named) returns.

All 6 comments

You also have to consider the case in which the result parameters have names.

@ianlancetaylor added result parameters have names.

Thanks.

I think it's a nice feature that the result parameter list looks like the argument parameter list.

The exception is that we permit a single unnamed result parameter to not use parentheses. I'm not seeing a good reason to extend that exception to the whole parameter list.

I don't think Go should enforce that you always need to use parenthesis around the return arguments.
But there's also nothing to prevent you from using that style yourself. Your example:

func A() (string){}
func B() (string, string){}
func C() (s string){}
func D() (s1 string, s2 string){}

Is already Valid go code, even though you could optionally drop the parens for single-value (non-named) returns.

Perhaps this would be better suited to go fmt rather than a language change. I'm personally a fan of consistency but I don't think that this change is worth breaking old programs.

The current syntax is familiar and simple. This proposal doesn't bring any significant advantage. It does not address any problem that people have writing Go.

Was this page helpful?
0 / 5 - 0 ratings