Skip to content

proposal: Go 2.0 - Allow the _ character to represent the default value for a given return type when returning from a function. #21422

Closed
@luke-park

Description

@luke-park

Go clearly defines what the "default" or "zero" value of a type is, whether it be 0, "", false, nil or whatever. With this in mind, consider the following code snippet:

func GetUserInformationFromEmail(email string) (bool, string, time.Time, error) {
    err := someFuncThatCouldRaiseAnError()
    if err != nil { return false, "", time.Time{ }, err }
    ...
}

I propose that we allow the _ character to represent the default, or "zero" value, of a given type when returning from a function. This would amend the original example to the following:

func GetUserInformationFromEmail(email string) (bool, string, time.Time, error) {
    err := someFuncThatCouldRaiseAnError()
    if err != nil { return _, _, _, err }
    ...
}

This change would reduce time spent checking return types and their default values (when I first started learning Go, returning time.Time{ } for an empty time really threw me). I feel this change would also be semantically meaningful with the current uses for the _ symbol.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions