Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

cannot call pointer method on "github.com/mholt/binding".Bind(c.Request, &form) #8

Closed
troyk opened this issue Jul 20, 2014 · 4 comments

Comments

@troyk
Copy link

troyk commented Jul 20, 2014

Checking out your binding lib, looks nice and I think it's going to save me a bunch of time! The README docs mention the ability to call Handle with Bind in a one-liner fashion, however (Go 1.3) I get an error. I annotated the error on the commented lines below, you can see I'm using the same exact arguments with the non-commented lines which compile fine.

Thanks again, Troy

    form := LoginForm{}

    err := binding.Bind(c.Request, &form)
    if err.Handle(c.Writer) {
        return
    }
    // causes cannot call pointer method on "github.com/mholt/binding".Bind(c.Request, &form)
    //if binding.Bind(c.Request, &form).Handle(c.Writer) {
    //  return
    //}
@mark-kubacki
Copy link
Contributor

That's a subtle one! Indeed, the README is not up-to-date on this. We have to go through an assignment for this to work (your non-commented code):

form := new(LoginForm)
if errs := binding.Bind(req, form); errs.Handle(resp) {
    return
}

see http://golang.org/ref/spec#Address_operators


  1. func (e *Errors) Handle(response http.ResponseWriter) bool {
  2. func Bind(req *http.Request, userStruct FieldMapper) Errors {

@mholt
Copy link
Owner

mholt commented Jul 31, 2014

Nice find. And @wmark thanks for pointing out the discrepancy.

I could change the Handle method to have a value receiver, which would enable that convenient, chained one-liner, rather than having to separate the assignment into a different statement. I tried this locally and confirmed that the example in the README still works, including that one line that is currently broken.

Making the change is simple. But I'm not sure if there are other implications of changing the receiver on that method from pointer to value. Can you think of any? If there aren't any obvious implications that I'm overlooking, I'll just make the change and resolve this issue.

@mark-kubacki
Copy link
Contributor

I don't see anything relevant here. Let's do it.

@mark-kubacki
Copy link
Contributor

Here's Rob Pike's comment on a related matter:

https://groups.google.com/d/msg/golang-nuts/INedfATw74A/XrIPT6gkB7kJ

@mholt mholt closed this as completed in 7255433 Jul 31, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants