Skip to content

confuse with "go vet error is not a type" #33668

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

Closed
wangcheng711 opened this issue Aug 15, 2019 · 2 comments
Closed

confuse with "go vet error is not a type" #33668

wangcheng711 opened this issue Aug 15, 2019 · 2 comments

Comments

@wangcheng711
Copy link

wangcheng711 commented Aug 15, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.7 linux/amd64

What did you do?

the sample code:

package main

import (
	"log"
	"os"
)

func main() {
	file, error := os.Open("hs.json")
	if error != nil {
		log.Println("open file ", error)
		return
	}
	defer file.Close()
	h := func(string) error {
		return nil
	}
	h("hello")

}

What did you see instead?

i am beginner with golang, the go vet output is 'error is not a type'. so, it can be confusing to fix problem

@bcmills
Copy link
Contributor

bcmills commented Aug 15, 2019

Go has a flat namespace: variable names and and type-names can (and do) shadow each other.

Here, you have declared a local variable error (on line 9), and it shadows the declaration of the built-in type error.

@bcmills
Copy link
Contributor

bcmills commented Aug 15, 2019

Duplicate of #33567

@bcmills bcmills marked this as a duplicate of #33567 Aug 15, 2019
@bcmills bcmills closed this as completed Aug 15, 2019
@golang golang locked and limited conversation to collaborators Aug 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants