This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 697
undefined errors.Wrap #225
Comments
Please check your imports, you’ve imported the standard library errors package, not this project.
… On 16 Mar 2020, at 04:55, Matteo Baiguini ***@***.***> wrote:
I was looking into new errors package functionalities and how to handle errors on defer, but I'm not able to build my very simple test.
Here the code:
package main
import (
"errors"
"fmt"
"os"
)
func main() {
goodErr := good()
if goodErr != nil {
fmt.Println("good:", goodErr.Error())
}
betterErr := better()
if betterErr != nil {
fmt.Println("better:", betterErr.Error())
}
bestErr := best()
if bestErr != nil {
fmt.Println("best:", bestErr.Error())
}
}
func good() error {
f, err := os.Open("./defer-error/book.txt")
if err != nil {
return err
}
defer func() {
err := f.Close()
if err != nil {
// do something like log
}
}()
// some other code
return nil
}
func better() (err error) {
f, err := os.Open("./defer-error/book.txt")
if err != nil {
return err
}
defer func() {
ferr := f.Close()
if ferr != nil {
err = ferr
}
}()
// some other code
return nil
}
func best() (err error) {
f, err := os.Open("./defer-error/book.txt")
if err != nil {
return err
}
defer func() {
ferr := f.Close()
if ferr != nil {
err = errors.Wrap(err, ferr.Error())
}
}()
// some other code
return nil
}
Here the error:
zsh$ go build ./...
# github.com/bygui86/go-defer-panic-recover/defer-error
defer-error/main.go:63:10: undefined: errors.Wrap
I really don't understand why it fails. Even using JetBrains Goland, it encounters the very same error.
Go version: 1.14
OS: macOS Mojave 10.14.6 (18G3020)
Shell: zsh
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Oh I'm really sorry! I changed it many times, but every time Goland saves it changes the import back to standard one... |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I was looking into new
errors
package functionalities and how to handle errors on defer, but I'm not able to build my very simple test.Here the code:
Here the error:
I really don't understand why it fails. Even using JetBrains Goland, it encounters the very same error.
What am I doing wrong?
Go version: 1.14
OS: macOS Mojave 10.14.6 (18G3020)
Shell: zsh
The text was updated successfully, but these errors were encountered: