Live data from Hacker News

Why I wrote gocraft/web

developer.uservoice.com

11–16 of 16 posts

Re: Why I wrote gocraft/web

#11
post #8

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?

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 tha…

So you're (ab?)using this to peg middleware functions on the context object? Why do it like that?

Re: Why I wrote gocraft/web

#12

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?

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

Re: Why I wrote gocraft/web

#13
post #8

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?

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 tha…

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?)

Re: Why I wrote gocraft/web

#14
post #9
post #6

Earlier quoted context omitted.

As another commenter mentioned, I learned about Martini midway through writing this. I really like Martini - if it existed a bit earlier, I probably would have just used it and been happy. That being said, there are some differences between the packages. First, Martini has great plugin support because 3rd parties can inject their arbitrary type into handlers. On the other hand, gocraft/web has nested routers and midd…

So Gocraft/web stays around ~4ms no matter how many routes you throw at it essentially but Martini and others are ~20ms and sometimes much more? That can really add up. Most web apps don't have quite so many routes typically but its not unreasonable and I'm surprised routing is allowed to get so expensive in the other frameworks. Kudos to you, I was psyched about Martini so I'm going to see about fixing that for it a…

Additionally, some of these libs are unoptimized and performance will change with time. Either way, since the performance differences are so small, it's probably best to go with what's easiest to work with.

Re: Why I wrote gocraft/web

#16

Just curious, but why have request be different from context? The request is the context.

While using gocraft/web I've slowly come to the realization that it would be very convenient if the Context already knew about the Request and Response (without me having to pass web.ResponseWriter everywhere).
Post reply on HN