Skip to content

Instantly share code, notes, and snippets.

@zhexuany
Last active February 10, 2020 20:20
Show Gist options
  • Save zhexuany/9d07c682d62a4542b5080d4f14a4a821 to your computer and use it in GitHub Desktop.
Save zhexuany/9d07c682d62a4542b5080d4f14a4a821 to your computer and use it in GitHub Desktop.
Go, X does not implement Y (… method has a pointer receiver)
package main
import (
"fmt"
)
type Foo interface {
Bar()
}
type Baz struct {
}
func (b *Baz) Bar() {
fmt.Println("this is Bar in Baz")
}
func A(f Foo) {
f.Bar()
}
func main() {
var f Foo
b := Baz{}
// This compile-time error arises when you try to assign
// or pass (or convert) a concrete type to an interface
// type; and the type itself does not implement the
// interface, only a pointer to the type.
f = &b
A(f)
// The following is wrong approach
// b := Baz{}
// A(b)
}
@zhexuany
Copy link
Author

zhexuany commented Mar 20, 2017

If anyone want to dive in deep, I just wrote a post about this. You can find it on this

@mcandre
Copy link

mcandre commented Feb 21, 2019

dead bloody link

@vituchon
Copy link

link is broken

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