V
V
Vladimir2020-01-01 21:25:45
go
Vladimir, 2020-01-01 21:25:45

How to use check/handle in Golang?

I tried to write handlers for the check statement in the Go 2 Error Handling article , but, for example, this code, taken from the article, does not compile:

func printSum(a, b string) error {
    handle err { return err }
    x := check strconv.Atoi(a)
    y := check strconv.Atoi(b)
    fmt.Println("result:", x + y)
    return nil
}


Mistake:

check-handle/main.go:13:12: syntax error: unexpected err at end of statement
check-handle/main.go:14:5: syntax error: non-declaration statement outside function body


No matter what I do, I can't write an error handler. Please, help. If it’s not difficult, please advise how best to handle errors, since I have a copy-paste from the error handler in my code, for example:

rawSubStart, err := parseTime(rawSub.Start)
if err != nil {
  return err
}
rawSubEnd, err := parseTime(rawSub.End)
if err != nil {
  return err
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2020-01-01
@vipho

Do you have version 2?
Hint: version 2 doesn't exist :) and the article describes ideas that might be added to Go someday in a future that hasn't happened yet.
in the article it says:

Design drafts are not even proposals (proposals) with which any change in the library, tooling or the Go language begins. This is a starting point for design discussion, proposed by the Go team after years of working on these issues. Everything that is described in drafts will most likely be changed, and, in the best scenario, will become reality only after a few releases (I give ~2 years).

If it’s not difficult, please advise how best to handle errors, since I have a copy-paste from the error handler in my code, for example:

Yes, and handle it, that's it, it's accepted here ... You can put it in a separate function that will return `start, end, err`.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question