Build Your Own Web Framework in Go
nicolasmerouze.com
nicolasmerouze.com
In the long run, when you start a web application using packages like "net/http" and the gorilla toolkit, sooner or later it becomes framework-like, because you start to group your code together, you refactor it and make it easiliy extendable _for the app you are writing_. So, every app becomes a framework on it's own. And what I like about Go is the fact, that building such mini framworks is easy, and does not take much time - but the most important aspect is, that you don't need a huge framework that does all the things.
Exactly. Very subjective, but I lean towards library with golang, because it seems more natural, and the bigger a framework gets, the more you have to do work for it to get it to do work for you.
That's not a bad thing. Delegation is a very useful composition pattern.
func(http.ResponseWriter, *http.Request)
I started with barebones http, then used gorilla, and later started using negrioni.Any new library with uses this function helps me.
So in the end: you dont need a framework, but libraries are nice.
Note: My api is a simple json based rest service.
GitHub: https://github.com/melling/GoWebServer
I'm not sure that I like embedding html in fmt.Print() methods, along with templates. A lot of my page handlers look something like this:
https://github.com/melling/GoWebServer/blob/master/src/webse...
My site is about 4000-5000 lines of Go, with a bit of "repeating myself". I'd like to hear from devs who've built larger sites.
Btw, here's my site:
And the usually "hidden" admin pages where I've done most of my work:
http://thespanishsite.com/admin/summary?language=chinese&pic...
[0]: https://www.youtube.com/watch?v=yi5A3cK1LNA&list=PLMW8Xq7bXr...
Building a framework is useful for doing that, because you have to understand the routing mechanisms, the way statuses and headers are set within the net/http package, etc.
It'll make the "should we throw this code out" discussion WAAAAY easier in five years.
> things nobody but me will ever use
Plus, I imagine I'll rewrite for example my CMS from scratch and/or in a different language every 5-10 years either way, taking a good hard look at what I really ended up needing, and how to best structure it. If I ever used a framework for that, I'd end up forking it and throwing out the 90% things it has which I don't need, cutting out redundant layers of indirection etc., and then what? The next version of the framework comes out, and I do it again? Or I just give up and just take on the bloat? Nah. Not worth it for me.
There is of course a huge leap between implementing and formalizing parts of a pre-built net/http package in Go and writing a well-designed language with a quality lexer/parser, but the idea is the same. Assuming you're not doing it to be your standard live environment language/framework of choice, it's a valuable process for learning.
If everyone took the :scoff: "don't write a framework, we already have them" approach it's true we'd have a lot fewer shitty frameworks. But we'd also have a lot fewer good ones.
Put it another way, Django and Rails would never have gotten as popular were it not for their 'batteries included' approach. Sure, Sinatra and Flask came, but they came after.
IMV, Go needs a Django/Rails if it is going to be a webframework. If not, if Go is destined for the API layer, it is probably fine with what it has now.
However I am the first voice against ORMs in any project I work on.
If you care about performance, nothing beats tuned prepared stated going through the wire and stored procedures for heavy duty work.
In a few projects we improved batch processing times by a few hours just by throwing the ORM into the garbage can.
for "heavy duty work",sure.Now write your own hydrators,again,again and again,and let's see how maintainable your code is.
Now the fact is ,it's impossible to write an ORM in Go,without throwing all type safety,just like Java pre-generics,you'd cast and down-cast to Ojbect.
Thanks for the tip.
I'm also surprised to read that Jeremy has written a more idiomatic successor framework... and named it "Negroni" (http://negroni.codegangsta.io). I don't know if I'm being too hyper-sensitive as a contemporary American, but is this just absolutely cringe-worthy to anyone else? I don't think I could evangelize this at work for that simple reason.
"Oh hey, go check out the 'Negro' framework! It's written by the guy who goes by 'Code Gangsta', and you can read about it on his blog in between YouTube clips of him rapping." (yes, seriously)
I mean it's a common drink.