What library do you need in golang to build a restful service?

polaris · · 758 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am mainly looking for a document page like github or official page, that have list of libraries I need for building Rest service. Coming from Django background, most of the stuff is tied into the framework so I am not exactly sure what I need either.</p> <p>From a high level perspective, I need</p> <pre><code>To save and retreive from database To get request and send response to Apache/nginx To serve content html/js/css </code></pre> <p>Here is what I found so far</p> <pre><code>URL Router -&gt; https://github.com/julienschmidt/httprouter how to hookup database -&gt; https://github.com/golang/go/wiki/SQLDrivers </code></pre> <p>Am I missing anything?</p> <p>Thanks</p> <hr/>**评论:**<br/><br/>tty5: <pre><ul> <li>database access: <a href="https://golang.org/pkg/database/sql/">https://golang.org/pkg/database/sql/</a> - standard library</li> <li>HTTP: <a href="https://golang.org/pkg/net/http/">https://golang.org/pkg/net/http/</a> - standard library</li> <li>static file serving: <a href="https://golang.org/pkg/net/http/#ServeFile">https://golang.org/pkg/net/http/#ServeFile</a> - standard library</li> <li>templates: <a href="https://golang.org/pkg/html/template/">https://golang.org/pkg/html/template/</a> - standard library</li> </ul> <p>I think by now you&#39;ve noticed a pattern here :P</p> <p>Also you don&#39;t need nginx/apache in front of go program - net/http is secure and highly performant</p></pre>warmans: <pre><p>I agree in general but I&#39;d say for a REST API a proper routing lib like gorilla mux is worthwhile. Having nice &#34;/resource/{id}/subresource/{id2}&#34; type routes definitions makes things cleaner IMO.</p></pre>tty5: <pre><p>The question OP asked was what libraries he <strong>needs</strong> to get this done and the answer is just the standard library. </p> <p>Considering how good the standard library is I consider this a valid, usable in real life answer. On the other hand I agree that there are packages adding some syntactic sugar / adding value on top of the ones I listed.</p> <p>All based on personal preference - there are plenty others:</p> <ul> <li>on top of net/http for nicer routing, middlewares, etc - any of: <ul> <li><a href="https://github.com/pressly/chi" rel="nofollow">https://github.com/pressly/chi</a></li> <li><a href="https://github.com/zenazn/goji" rel="nofollow">https://github.com/zenazn/goji</a></li> <li><a href="https://github.com/labstack/echo" rel="nofollow">https://github.com/labstack/echo</a></li> </ul></li> <li>on top of database/sql: <ul> <li><a href="https://github.com/gocraft/dbr" rel="nofollow">https://github.com/gocraft/dbr</a> - query builder, easier db&lt;-&gt;struct, doesn&#39;t get in your way</li> <li><a href="https://github.com/jmoiron/sqlx" rel="nofollow">https://github.com/jmoiron/sqlx</a> - nice wrapper around database/sql</li> </ul></li> </ul></pre>dbrecht: <pre><blockquote> <p>Also you don&#39;t need nginx/apache in front of go program - net/http is secure and highly performant</p> </blockquote> <p>That is, unless you have a cluster of app servers and you&#39;re using nginx&#39;s load balancing</p></pre>tty5: <pre><p>true, but in that case you&#39;re better off with HAProxy, Traffic Server or even Tengine (nginx fork with upstream health checks + a lot of other nice stuff)</p></pre>izuriel: <pre><p>Really glad someone responded with this information. Sure third party libraries are great but they can do a lot you don&#39;t care about. I think it&#39;s best to start with the standard library and then if you really need a feature from a third party, only then should you invest. </p> <p>Also I&#39;d say avoid any routing or HTTP wrapper library that breaks the standard libraries method signatures. Gorilla is broken into chunks that allow you to do things without buying into library specific types you&#39;ll never be able to use elsewhere. </p></pre>neoasterisk: <pre><p>The standard library has everything you need to accomplish all these tasks you mentioned, so you will not need anything extra (except the driver). My recommendation would be to stick with the standard library and only use external packages if there is a very good reason.</p></pre>smantziaris: <pre><p>+1.</p></pre>nathj07: <pre><p>The only thing I would add is a routee, gorilla mux is good or the httprouter package you mention. Later in you may want something to help pass context. Other than that the STD lib is awesome</p></pre>artpar: <pre><p>Yes, Gorilla or gocraft/web mux is really useful, and just a library. </p></pre>hockeyhippie: <pre><p>I&#39;ve been using this lately and have been pretty happy with it: <a href="http://ant0ine.github.io/go-json-rest/" rel="nofollow">http://ant0ine.github.io/go-json-rest/</a></p></pre>karnd01: <pre><p>There are several http router libraries that would definitely help, </p> <p>LARS - <a href="https://github.com/go-playground/lars" rel="nofollow">https://github.com/go-playground/lars</a> Echo - <a href="https://github.com/labstack/echo" rel="nofollow">https://github.com/labstack/echo</a> httptreemux - <a href="https://github.com/dimfeld/httptreemux" rel="nofollow">https://github.com/dimfeld/httptreemux</a></p> <p>A data validation library, prior to saving data to database: validator - <a href="https://github.com/go-playground/validator" rel="nofollow">https://github.com/go-playground/validator</a></p> <p>A CORS library <a href="https://github.com/rs/cors" rel="nofollow">https://github.com/rs/cors</a></p> <p>A word of caution about httprouter and other based on it&#39;s algorithm, they are fast but currently lack support for some pretty common URL patterns <a href="https://github.com/julienschmidt/httprouter/issues/73" rel="nofollow">https://github.com/julienschmidt/httprouter/issues/73</a> the ones I listed all handle those patterns</p></pre>j1436go: <pre><p>Goji is really minimal and useful</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

758 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传