0

This is my code:

package main

import "fmt"

type Group struct {
}

func (g *Group) FooMethod() string {
    return "foo"
}

type Data interface {
    FooMethod() string
}

func NewJsonResponse(d Data) Data {
    return d
}

func main() {
    var g Group
    json := NewJsonResponse(g)
    fmt.Println("vim-go")
}

but does not work as I expect.

$ go build main.go
# command-line-arguments
./main.go:22: cannot use g (type Group) as type Data in argument to NewJsonResponse:
    Group does not implement Data (FooMethod method has pointer receiver)
sensorario
  • 20,262
  • 30
  • 97
  • 159
  • 1
    See [Go, X does not implement Y (... method has a pointer receiver)](https://stackoverflow.com/questions/40823315/go-x-does-not-implement-y-method-has-a-pointer-receiver/40824044#40824044) for detailed explanation and possible solutions. – icza Aug 10 '17 at 04:10

1 Answers1

1

If you want to use a struct receiver, remove the * from before Group in the definition of your function on line 8. As a convenience they do work the other way round (defined on struct works on a pointer receiver). See effective go for an explanation.

https://golang.org/doc/effective_go.html#pointers_vs_values

Modified version:

https://play.golang.org/p/ww6IYVPtIE

Kenny Grant
  • 9,360
  • 2
  • 33
  • 47