Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A idea: Improve the error check and change it to a language keyword #47435

Closed
ssoor opened this issue Jul 28, 2021 · 2 comments
Closed

A idea: Improve the error check and change it to a language keyword #47435

ssoor opened this issue Jul 28, 2021 · 2 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@ssoor
Copy link

ssoor commented Jul 28, 2021

We propose a new syntax: !error, specifically designed to eliminate if boilerplate statements that are usually associated with error handling in Go.

func open(filename string) (*file,error) {
    return ..., errors.New("file does not exist")
}

f, err := open(filename)
if err != nil {
	return …, err  // zero values for other results, if any
}

can be simplified to

func open(filename string) *file!error {
    return !errors.New("file does not exist")
}

f, ! := os.Open(filename)

Complete example:

func f(v1 int,v2 int) (int,int)!error {
    if v1 < 0 {
        return !errors.New("error...") // Return a error, other results return the default zero value
    }

    return v1+v2, v1-v2
}

func print1(v1 int,v2 int) !error {
    add, sub, ! := f(v1, v2) // If the error is not empty, it will return the error
    fmt.Println(add, sub)
}


func print2(v1 int,v2 int) !error {
    add, sub, !err := f(v1, v2) // If the error is not empty, it will be assigned to err
    fmt.Println(add, sub)

    if err != nil {
         return !err // return the error
    }

    return !nil
}

func main() { // The method is not allowed to return an error, the error must be assigned to the variable
    !err := print1(-1,1)
    fmt.Println(err) 
    // error...

    !err := print2(-1,1)
    fmt.Println(err)
    // 0 0
    // error...

    !err := print1(1,2)
    fmt.Println(err)
    // 3 -1
   //  nil
}

It borrows ideas from:

@andig
Copy link
Contributor

andig commented Jul 28, 2021

Youll need to fill the language change proposal

@mvdan
Copy link
Member

mvdan commented Jul 28, 2021

Please note that you should fill https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing a language change.

@mvdan mvdan added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 28, 2021
@ssoor ssoor closed this as completed Jul 29, 2021
@golang golang locked and limited conversation to collaborators Jul 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants