Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

undefined errors.Wrap #225

Closed
bygui86 opened this issue Mar 15, 2020 · 2 comments
Closed

undefined errors.Wrap #225

bygui86 opened this issue Mar 15, 2020 · 2 comments

Comments

@bygui86
Copy link

bygui86 commented Mar 15, 2020

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.
What am I doing wrong?

Go version: 1.14
OS: macOS Mojave 10.14.6 (18G3020)
Shell: zsh

@davecheney
Copy link
Member

davecheney commented Mar 15, 2020 via email

@bygui86
Copy link
Author

bygui86 commented Mar 16, 2020

Oh I'm really sorry! I changed it many times, but every time Goland saves it changes the import back to standard one...
Thanks a lot!

@bygui86 bygui86 closed this as completed Mar 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants