Last active
February 10, 2020 20:20
-
-
Save zhexuany/9d07c682d62a4542b5080d4f14a4a821 to your computer and use it in GitHub Desktop.
Go, X does not implement Y (… method has a pointer receiver)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
dead bloody link
link is broken
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If anyone want to dive in deep, I just wrote a post about this. You can find it on this