Skip to content

proposal: Go 2: improve if statement #26712

@gbbr

Description

@gbbr

This proposal is for considering the introduction of a new form to the if statement by allowing not only expressions, but also an interface. It would look like this:

"if" ( Expression | InterfaceType) Block

This would change the if statement's behavior in such a way that when an interface argument is provided (as opposed to an expression), the statement would evaluate it as false if the interface is nil or if its value is nil.

This could maybe solve the problem when newcomers assume wrongly about err == nil (see FAQ entry) and offer a good solution to performing this check (#22729). Example:

if !v {
    // v is an interface with value nil or holds a nil pointer
    // it is unusable
}

It also attempts simplifying the (by some considered tedious) error handling (#21161). It would become:

if err {
   // handle error
}

However I'm unsure if this has any side effects which could cause confusion or wrong assumptions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageChangeSuggested changes to the Go languageProposalerror-handlingLanguage & library change proposals that are about error handling.v2An incompatible library change

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions