TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A RESTful Micro-Framework in Go

234 pointsby Nogwaterover 11 years ago

13 comments

mitchellhover 11 years ago
This looks like a great resource! A bit of a rant (not directed at the person who wrote this, because the link at hand is great):<p>Writing APIs in Go is not the hard part. There are a ton of options for routing basic URLs to handlers. There are a few areas that are a nightmare, total total nightmare:<p>* SQL - The raw database&#x2F;sql package in Go is very low-level. It is a terrible experience to use in a project that makes anything more than a handful of simple queries. I&#x27;m not asking for an ORM, but it would really benefit the community if there was a SQLAlchemy style (the non-ORM part of it) library for Go. It should be MUCH easier to make basic queries to SQL and get out results.<p>* Templating - The built in &quot;html&#x2F;template&quot; package is just not good enough for a web app when compared to something like Jinja (Python). &quot;html&#x2F;template&quot; gets points for being simple, and actually its not a complaint against &quot;html&#x2F;template&quot;: I think there just needs to be a more feature-packed templating package like Jinja for Go. &quot;html&#x2F;template&quot; doesn&#x27;t support things like inheritance, didn&#x27;t even support comparison operators until Go 1.2, etc.<p>These two problems have made it so I&#x27;ve pretty much given up web apps in Go for now. Services in Go are _amazing_. But if you&#x27;re trying to get a full blown web app (again, not the point of the linked article above), then you&#x27;re gonna have a bad time.
评论 #7126465 未加载
评论 #7126220 未加载
评论 #7126179 未加载
评论 #7128838 未加载
评论 #7127919 未加载
评论 #7126026 未加载
评论 #7127163 未加载
评论 #7126191 未加载
tptacekover 11 years ago
This looks like great work, so probably this comment is a little off topic, but here goes anyways:<p>Every web framework I&#x27;ve seen for Golang seems to do a competent job of tackling the easiest part of designing a web framework, which is routing requests to handlers.<p>Actually, the built-in net&#x2F;http library already does a serviceable job of handling requests, and if you understand closures, filtering them and maintaining request state as well.<p>The thing no framework I have seen so far addresses is persistence. SQL persistence in Golang is a bit of a nightmare. It&#x27;s raw SQL, and the database libraries are fussy.<p>The thing that makes Rails so easy to write in, especially for newcomers, is ActiveRecord. As far as I can tell, nothing like it exists (in a stable form) for Golang.
评论 #7125922 未加载
评论 #7125908 未加载
评论 #7126002 未加载
评论 #7125938 未加载
评论 #7128220 未加载
评论 #7125872 未加载
评论 #7125925 未加载
评论 #7125892 未加载
natural219over 11 years ago
This post really highlights what I like about Go so far vs other languages -- the evolving community. Like Rails, there is an emphasis on abstraction and productivity. Unlike Rails, the blog posts I&#x27;ve seen so far seem to be centered around <i>explaining</i> how to implement the functionality instead of just <i>wrapping</i> the functionality and providing an easy API. Now that I&#x27;ve read this post, I could download his library, implement my own, or extend the parts I want to customize very easily. I feel more powerful.
polymathistover 11 years ago
I would have taken a different approach for handling unsupported resource methods. I&#x27;d love to hear some feedback on whether or not this is considered idiomatic. Instead of this:<p><pre><code> type HelloResource struct { sleepy.PostNotSupported sleepy.PutNotSupported sleepy.DeleteNotSupported } </code></pre> Something like this:<p><pre><code> type HelloResource struct { sleepy.DefaultResponder } </code></pre> DefaultResponder is simply a struct which has all the methods of sleepy.PostNotSupported, sleepy.PutNotSupported, etc, and is defined:<p><pre><code> type DefaultResponder struct{} func (DefaultResponder) Get(values url.Values) (int, interface{}) { return 405, map[string]string{&quot;error&quot;: &quot;Not implemented&quot;} } func (DefaultResponder) Post(values url.Values) (int, interface{}) { return 405, map[string]string{&quot;error&quot;: &quot;Not implemented&quot;} } func (DefaultResponder) Put(values url.Values) (int, interface{}) { return 405, map[string]string{&quot;error&quot;: &quot;Not implemented&quot;} } func (DefaultResponder) Delete(values url.Values) (int, interface{}) { return 405, map[string]string{&quot;error&quot;: &quot;Not implemented&quot;} } </code></pre> Now, if you want to implement resource methods, you define them on the top-level struct, like so:<p><pre><code> type HelloResource struct { sleepy.DefaultResponder } def (HelloResource) Get(values url.Values) (int, interface{}) { data := map[string]string{&quot;hello&quot;: &quot;world&quot;} return 200, data } </code></pre> So now, if you call Get on a HelloResource type, it uses the method defined on HelloResource, not the one defined on it&#x27;s embedded DefaultResponder type. But if you call HelloResource.Post, it uses the method defined on DefaultResponder, i.e., it returns a 405 error. This is effectively like &quot;overriding&quot; in Java or other object oriented languages and saves a few lines when people are using your library.<p>What do you think? Again, I&#x27;m really just looking for feedback here as I too am new to go. Is this something that would be considered idiomatic, or is the original solution better?<p>(Apologies in advance for any typos or syntax errors. Didn&#x27;t check this with a compiler, but hopefully you get the idea.)
评论 #7129987 未加载
_akover 11 years ago
Nice, easy to use, really straightforward code. The only thing I would have done differently is make sleepy.Api implement http.Handler so that it would easily integrate with other libraries that build upon and play nicely with net&#x2F;http.
dclaraover 11 years ago
I haven&#x27;t seen the full picture of Go yet, partially by some examples. I found it makes calling multi-threading functions a lot easier.<p>I consider Ruby, Python, Go and Julia are the new generation of programming languages aiming to be quickly picked up and made hands-on working.<p>Can anybody tell me if Ruby, Python, Go supports a full spectrum of functionalities for various network protocols, multi-threading, messaging (sync and async) and concurrent accessing etc. Those kind of functionalities don&#x27;t only require additional libraries, but also frameworks and algorithms.<p>Maybe by using Jython, JRuby or something like that, they are able to achieve the complete functionalities like Java.<p>According to some research, Python evolved from Lisp, and Ruby is closed to Perl. So why aren&#x27;t those script-based language more popular than Java&#x2F;C++?
mjalldayover 11 years ago
Languages live or die on their libraries.<p>This is great. I sat down to write a quick api in go about three weeks ago but couldn&#x27;t find a framework that I wanted to use. This looks like it has a decent abstraction and libraries like this really help language adoption.<p>Much thanks to the author of the library.
jevinskieover 11 years ago
I appreciated the development narrative rather than a post-project summary. Thanks!
jrockwayover 11 years ago
Drive-by code review:<p>I don&#x27;t see any reason for the Api type to exist. Just make them normal functions?<p>It would also be helpful to print the portstring rather than just &quot;Hi.&quot;.
cronosover 11 years ago
Why exactly do Resource&#x27;s methods take variadic argument? In what scenario can there be more than one?<p>Also, small remarks about code: You define type HandleFunc in your package which is identical to http.HandlerFunc. Looks like it can be removed. Some error logging (rw.Write, json.Marshal) would be useful as well.
评论 #7125829 未加载
swahover 11 years ago
Kinda off-topic, but would you write a real-time web application in Go today?
评论 #7125762 未加载
评论 #7125850 未加载
dark_ph0enixover 11 years ago
I&#x27;m yet to test this, but any idea how it compares against Martini?
评论 #7126759 未加载
评论 #7126547 未加载
skjover 11 years ago
Wonderfully small, easy to grok, and put into practice.