Error "X does not implement Y (method has a pointer receiver)" in Go
Hello! I started learning Go and encountered a strange error when working with interfaces. I have a simple `Speaker` interface with a `Speak()` method, and a `Dog` struct that implements this method. But when I try to pass a `Dog` value to a function expecting `Speaker`, the compiler gives an error:
```
cannot use dog (variable of type Dog) as Speaker value in argument to makeSpeak: Dog does not implement Speaker (method Speak has a pointer receiver)
```
Here is my code:
```go
package main
import "fmt"
type Speaker interface {
Speak() string
}
type Dog struct {
Name string
}
func (d *Dog) Speak() string {
return "Woof! I am " + d.Name
}
func makeSpeak(s Speaker) {
fmt.Println(s.Speak())
}
func main() {
dog := Dog{Name: "Bobik"}
makeSpeak(dog)
}
```
No answers yet.
Log in to leave an answer