Implementing an interface: What am I doing wrong?

1,313 views
Skip to first unread message

Sean Chitwood

unread,
Mar 27, 2012, 10:33:55 AM3/27/12
to golan...@googlegroups.com
I'm using Go to implement a simple MVC app on AppEngine and I'm getting a compilation error for one of types that is supposed to implement the View interface:

cannot use jv (type util.JsonView) as type util.View in assignment:
util.JsonView does not implement util.View (Render method requires pointer receiver)


I can't see what I'm doing wrong, I have implemented the interface several times already. I've even tried altering how JsonView is implemented, using a slice of bytes rather than an interface but that didn't do it. Am I just missing something core?

My interface:
type View interface {
   Render(w http.ResponseWriter) (os.Error)
}


type that doesn't work:

type JsonView struct {
   Value interface {}
}

func (j *JsonView) Render(w http.ResponseWriter) (os.Error) {
  b, _ := json.Marshal(j.Value)
  _, err:= w.Write(b)
  return err
}

type that does work:

type JsonSuccessView struct { }

func (j *JsonSuccessView) Render(w http.ResponseWriter) (os.Error) {
  m := make(map[string] bool)
  m["success"] = true
  b, _ := json.Marshal(m)
  _, err:= w.Write(b)
  return err
}

Jesse McNelis

unread,
Mar 27, 2012, 10:40:36 AM3/27/12
to Sean Chitwood, golan...@googlegroups.com
On Wed, Mar 28, 2012 at 1:33 AM, Sean Chitwood <dark...@gmail.com> wrote:
> I'm using Go to implement a simple MVC app on AppEngine and I'm getting a
> compilation error for one of types that is supposed to implement the View
> interface:
>
> cannot use jv (type util.JsonView) as type util.View in assignment:
> util.JsonView does not implement util.View (Render method requires pointer
> receiver)

The problem is in the code you're not showing us.
Somewhere (the line that error message indicates) you are attempting
to assign a value of type util.JsonView to an util.View.
You want a *util.JsonView instead since *util.JsonView satisfies that
interface and util.JsonView doesn't.

>
>
> I can't see what I'm doing wrong, I have implemented the interface several
> times already. I've even tried altering how JsonView is implemented, using a
> slice of bytes rather than an interface but that didn't do it. Am I just
> missing something core?
>
>
> My interface:
>
> type View interface {
> Render(w http.ResponseWriter) (os.Error)
> }
>
>
>
> type that doesn't work:
>
>
> type JsonView struct {
> Value interface {}
> }
>
>
> func (j *JsonView) Render(w http.ResponseWriter) (os.Error) {
> b, _ := json.Marshal(j.Value)
> _, err:= w.Write(b)
> return err
> }
>
>
> type that does work:
>
>
> type JsonSuccessView struct { }
>
>
> func (j *JsonSuccessView) Render(w http.ResponseWriter) (os.Error) {
> m := make(map[string] bool)
> m["success"] = true
> b, _ := json.Marshal(m)
> _, err:= w.Write(b)
> return err
> }

--
=====================
http://jessta.id.au

chris dollin

unread,
Mar 27, 2012, 10:42:27 AM3/27/12
to Sean Chitwood, golan...@googlegroups.com
On 27 March 2012 15:33, Sean Chitwood <dark...@gmail.com> wrote:
> I'm using Go to implement a simple MVC app on AppEngine and I'm getting a
> compilation error for one of types that is supposed to implement the View
> interface:
>
> cannot use jv (type util.JsonView) as type util.View in assignment:
> util.JsonView does not implement util.View (Render method requires pointer
> receiver)

We need to see the line that fails (and the contrasting one that succeeds),
but my guess is that in one of them you have a *Wossname and in the other
just a Wossname. It's the *Wossname that satisfies the interface, not the
Wossname on its own.

(Wossname = JsonView / JsonSuccessView, as appropriate.)

Chris

--
Chris "allusive" Dollin

Sean Chitwood

unread,
Mar 27, 2012, 10:52:51 AM3/27/12
to golan...@googlegroups.com, Sean Chitwood
Thanks all. I'm still learning Go, which is half the reason I've been doing this project:

I replaced
 var jv util.JsonView

with

jv := new(util.JsonView)

and everything is peachy.
Reply all
Reply to author
Forward
0 new messages