New to Golang: What's the best best way to write a simple HTTP JSON API ?

agolangf · · 505 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Let&#39;s say I&#39;m building an incredibly simple JSON Web API.</p> <ul> <li>It has a few routes that respond to <code>GET</code> and <code>POST</code></li> <li>It responds with JSON</li> </ul> <p>If I were developing in Ruby or Python, I&#39;d use a really simple web framework like Sinatra or Flask. </p> <p>I&#39;m brand new to Go and I&#39;ve been tasked with implementing this basic setup. Is there an equivalent framework in Go? Or would I event need a framework? (I&#39;m guessing yeah since a framework will take care of parsing URL query parameters and such right?)</p> <p>Or is it easy enough to use a standard Net/HTTP library to parse an incoming web request and then respond with JSON?</p> <p>Would love if the community could point a newbie in the right direction.</p> <p>Thank you!</p> <hr/>**评论:**<br/><br/>acln0: <pre><p>If you&#39;re brand new to Go, be sure to start with the Go tour. Read Effective Go after that, then dive into the standard library.</p> <p>Don&#39;t use a framework. Use <code>net/http</code> and, perhaps, a third party request multiplexer if you want conveniences such as multiplexing based on HTTP method and having path parameters be parsed for you. <a href="https://godoc.org/github.com/gorilla/mux" rel="nofollow">gorilla/mux</a> and <a href="https://github.com/go-chi/chi" rel="nofollow">go-chi/chi</a> are both reasonable choices.</p></pre>UnderwaterPenguin: <pre><p>thank you!</p> <p>Will read effective go.</p></pre>spiritofthetempest: <pre><p>Don&#39;t use a framework your first time around. It took me several projects to realize that net/http and encoding/json have everything you need for the majority of web apps. The Golang standard library is great, and I think a selling point of Go is that we often don&#39;t need to reach for third party tools when the Stl and a tiny bit of extra code will do.</p></pre>UnderwaterPenguin: <pre><p>Oh that&#39;s good to know, thank you!</p></pre>tmornini: <pre><p>I generally agree with <a href="/u/spiritofthetempest" rel="nofollow">/u/spiritofthetempest</a>, but I&#39;ll tip my hat to:</p> <pre><code>https://github.com/julienschmidt/httprouter </code></pre> <p>It&#39;s a tiny, well tested, high performance library that suits my needs really, really well.</p></pre>needittobenotshit: <pre><p>Im in the same boat as you, cause Im learning Go I&#39;ve been sticking with standard library only. so just net/http. Its been very enjoyable thus far, I doubt its the best way though.</p></pre>

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

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