Skip to content

proposal: Go 2: add check err goto handler #37035

@noErrorNoLife

Description

@noErrorNoLife

I had an Epiphany last night like this:

func readAllText(path string) string {
	buffer, err := ioutil.ReadFile(path)

	if err != nil {
		goto handler
	}

	return string(buffer)

handler:
	{
		fmt.Println("error: ", err.Error())
		return ""
	}
}

Only the second value is detected each time, and if more than one value is returned, it is recommended to encapsulate it as a type,
If you provide a keyword to detect whether the second variable is nil like this:

a,err := method()
check err goto handler
b,err := method2()
if err != nil{
  // Special error handling...
}
handler:
	{
		fmt.Println("error: ", err.Error())
	}

This makes it compatible with previous or future error handling,Can you solve the problem of error handling
Finally , I'm sorry for my poor English

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