Just write the function/method/whatever such that it takes its
dependencies. There's no need for a fancy framework.
If you find yourself wanting reflection on day 1, ask us about what
you're up to. Chances are there's another approach that will be
simpler and faster.
On Wednesday, November 27, 2013 10:50:59 AM UTC-8, David C Cohen wrote:The problem is that, this handlers grouping comes with the cost of losing compile time safety, and runtime performance. This makes the point of chosing go as the programming language almost void.
Although I am not a fan of frameworks, I don't think these issues are a concern.
A framework that uses reflection can validate the registered handlers at startup and exit or panic with a meaningful error. This is not as good as a compile time error, but it's not bad.The cost of invoking a a handler using reflection is small compared to the cost of handling the HTTP request, fetching data from a database and so on.
It honestly depends on what problems you are solving. I firmly believe that reflection can lead to simpler code, but there is also a lot of nasty uses of reflection out there.There will be tradeoffs on both sides. I wrote Martini to be productive and to give me a better way to separate concerns in the application. Some complain about reflection overhead, which can range anywhere between 500ns and 2000ns per request depending on how much middleware you have. We are talking nanoseconds here! I can confidently say that if you cannot give up the 2 microseconds of overhead that Martini reflection provides then it is not for you.
You write this as if reflection is so slow that it turns Go into Ruby/PHP.
Reflection should always be used tastefully. Not everyone will have the same tastes. But in many cases, using a little bit of reflection imparts a negligible performance penalty.
So perhaps the overhead in using reflection, in the context of this discussion, is not that big a deal, but is it the best way to learn how to think like a Go programmer?
Hi,
What would a large web app written in idiomatic go look like? Should the handlers be grouped based, for instance in a RESTful fashion, or should they be listed flatly in a very long file?
So why do these frameworks use reflection? Because they try to implement an MVC pattern. In an MVC pattern, the http handlers are grouped as the methods of the controller struct. Grouping is good to keep the code organized, and helps with creating RESTful routing with less code.
The problem is that, this handlers grouping comes with the cost of losing compile time safety, and runtime performance. This makes the point of chosing go as the programming language almost void.
The final question is, is there a way of achieving http handlers grouping without the use of reflection? What are those 'other approaches' that Rob mentioned which could make this 'simpler and faster'?
Interesting. What would you say is the main advantage of your approach vs this?
You can use One single handler to dispatch to other handlers removing
the need to use reflection (yes, this is just a router like
gorilla/mux).
type handlerMap struct {
A, B, C http.Handler
}
Nice approach. Request specific dependencies are stored directly on the http.Request, as form values?
I use the Struct/MethodReference technique combined with a filter implementation and Gorilla/mux + context to achieve pretty much the same results.
There is another approach, and it is possibly the simplest one of all. Treat the HandlerFuncs/Handlers as a thin "glue" layer that creates and wires up services/actions in any manner required and sets them to work. Just a bridge/adapter between the web and your application. No fancy tricks, but it gets the job done. And it is very explicit.
Method 2: The so called frameworks
Web frameworks (if you can name them so) in go are growing like mushrooms. 99% of these frameworks use reflection.