Hacker News new | ask | show | jobs
by nemothekid 4566 days ago
I've been using go for some time but I've never seen this:

    Get("/hello", (*Context).SayHello)
What is going on here? How is (*Context).SayHello being called?
2 comments

I'm new to this syntax too, but I believe it's just a reference to the function `SayHello` as defined on the `Context*` type. That is, if you have:

    func FuncA() { } // normal function
    func (int) FuncB() { } // function on an int object
You could later reference those functions via:

   var func1 := FuncA // assign FuncA into func1 var
   var func2 := (int).FuncB // assign FuncB into func2 var
But again, I'm just piecing that together from context and haven't looked at the spec.
So you're (ab?)using this to peg middleware functions on the context object? Why do it like that?
I was missing that piece, but how is func2 called? I'm still looking through the code, but if both func2 and a int_variable are interface{}, how does the compiler check these types (or is the "framework" using some reflection?)
This is calling the method assigned to a Context, which would be defined as func (c *Context) Something(whatever) {} If you have a w Context variable, you could do w.SayHello, with this you just call the function without an intervening variable, straight from the definition