Skip to content

proposal: Go 2: Error-Handling Paradigm with !err Grammar Sugar #63961

@xiaokentrl

Description

@xiaokentrl

Hi, if you like this idea please give a heart emoji to me, Thank you very much

func copy(src, dst string) (int64, error) {

    r, err := os.Open("input.txt")
    if err != nil {
        return 0, err
    }
    defer r.Close()
 
    w, err := os.Create("output.txt")
    if err != nil {
        return 0, err
    }
    defer w.Close()
    
    if _, err = io.Copy(w, r); err != nil {
        return 0, err
    }
    return 0, err
}

Add keywords listen:

listen: err {
    if err != nil  {
        return 0, err
    }
    return 0, nil
}()

func copy(src, dst string) (int64, error) {
    listen:
    
    r, err := os.Open("input.txt")
    defer r.Close()
 
    w, err := os.Create("output.txt")
    defer w.Close()

    _, err = io.Copy(w, r) 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageChangeSuggested changes to the Go languageProposalWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.error-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