Where to learn how to use net/http's ServerMux

xuanbao · · 240 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve been trying to find a tutorial, or guide, or something to read about how to use GO&#39;s net/http ServerMux. However, when I search for something about that, or handling REST methods with GO, I usually find guides or tutorials for other Muxers like Gorilla, or httprouter. Is the net/http ServerMux just not able to handle it by itself, and I should use a 3rd party library, or am I just not understanding how to use the ServerMux? </p> <hr/>**评论:**<br/><br/>koalefant: <pre><p>From what I understand each server mux has a different way of implementing routing. The GitHub httprouter page has a pretty useful comparison of their mux to the default one.</p> <p>It&#39;s a preference thing, that also has some performance differences too. In the end though it&#39;s unlikely the mux is going to be your bottleneck so basically choose one that suits your needs?</p></pre>predatorian3: <pre><p>Thanks for your comment. Looking at HttpRouter it makes me think of other kinds of REST http servers that I&#39;ve used in languages like Ruby or Python. </p> <p>I just didn&#39;t understand how to use ServerMux. I suppose I could use if statements looking for the request methods specific to what I want</p></pre>nhooyr: <pre><p><a href="https://godoc.org/net/http#ServeMux" rel="nofollow">https://godoc.org/net/http#ServeMux</a></p></pre>predatorian3: <pre><p>When I read that excerpt, I&#39;m not sure what I need to do with it. That&#39;s my problem.</p></pre>Sphax: <pre><p>do you have something specific in mind ? because your question is kinda vague right now</p></pre>alecthomas: <pre><p>IMO <code>net/http.ServeMux</code> is flawed enough to basically only be useful for simple static routes, and even then it can be annoying (see point 2 below):</p> <ol> <li>It does not support path parameters (eg. <code>/resource/{id}/subresource/{srid}</code>)</li> <li>Trailing slashes match anything under that root. For example, <code>/resource/</code> would match <code>/resource/foo</code>, <code>/resource/foo/bar</code> and so on. This adds extra unnecessary work.</li> </ol></pre>KenjiTakahashi: <pre><p>Given that 1., 2. actually makes sense. That way you can implement parameters inside the handler. Agreed that this would probably quickly turn tedious to work with, though.</p></pre>jrwren: <pre><p>Its reasonably easy enough to use multiple instances to support this yourself.</p></pre>predatorian3: <pre><p>This is a good thing to consider. I haven&#39;t professed to using variables in my URL path</p></pre>tex0: <pre><p>net/http is very well suited for real world production applications. You can handle any type of parameters with a little extra code. It may require a little more effort than other routers, but it&#39;s dead simple.</p></pre>

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

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