Skip to content

Error cannot convert type <*main.b> to interface <error> on valid Go code #246

Description

@codemicro

When attempting to run the following snippet of Go code in the latest version of gophernotes, v0.7.4, I get the following error:

import "fmt"

type b struct {}
func (*b) Error() string { return "hi" }

func main() {
        var e error
        e = &b{}

        fmt.Println(e)
}
repl.go:8:9: error compiling assignment: e = &b{}
	repl.go:8:9: cannot convert type <*main.b> to interface <error>: missing method  Error

However, running essentially the same snippet with a package main declaration at the top yields the correc output.

package main

import "fmt"

type b struct {}
func (*b) Error() string { return "hi" }

func main() {
        var e error
        e = &b{}

        fmt.Println(e)
}
> go run main.go 
hi

:)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions