Skip to content

proposal: Go 2: make named return parameter assignment on last line a terminating statement of a function #31630

Description

@StephanVerbeeck

Consider the following 2 alternatives for implementing the same function that returns a result value. The second syntax is my favorite and I use it throughout my code (I am currently developing a commercial accounting application in GOlang (based on lxn/walk)).

func Sum(a, b int) int {
	return a + b
}

func Sum(a, b int) (total int) {
	total = a + b
}

But when using the predefined variable it seems very illogical to me that GO complains about leaving out the final return statement so I must correct this by doing this:

func Sum(a, b int) (total int) {
	total = a + b
	return
}

My proposal is to NOT generate the "missing return at end of function" error in the latter case allowing programmers to leave out this obviously superfluous line of code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions