What 3rd party libs are clearly better than the standard library?

agolangf · · 495 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>http.ServeMux is a good example.</p> <p>Edit: Yea, I&#39;m Unsubscribing from this sub. Even reasonable opinions of mine are just being down voted, and it looks like I&#39;m being personally insulted too. This isn&#39;t supposed to be <a href="/r/politics" rel="nofollow">/r/politics</a></p> <hr/>**评论:**<br/><br/>neoasterisk: <pre><p>This fud that <code>http.ServeMux</code> is not good enough or even bad, needs to stop.</p></pre>burtgummer45: <pre><p>So how do you do params?</p></pre>shovelpost: <pre><p>Here&#39;s <a href="https://youtu.be/yi5A3cK1LNA?t=11m44s" rel="nofollow">a way to do it</a>. In case you do not recognize the speaker, he is the creator of <a href="https://github.com/bmizerany/pat" rel="nofollow">pat</a>, a small but still popular http muxer which he admits he doesn&#39;t use anymore.</p></pre>burtgummer45: <pre><p>Thanks, that convinced me to never use http.ServeMux</p></pre>shovelpost: <pre><p>How so?</p></pre>burtgummer45: <pre><p>Lack of features? </p></pre>shovelpost: <pre><p>You fall into the fallacy described in the video as Predetermination. You have already decided that you will forever use an external router because <code>http.ServeMux</code> &#34;lacks features&#34;. This is not what I would call good engineering. </p> <p>Wise words by <a href="/u/carsncode" rel="nofollow">/u/carsncode</a>:</p> <blockquote> <p>My general recommendation though would have to be: don&#39;t take on dependencies unless you have to. The standard library is very broad, the language is very easy to work with, and personally I think the import-happy culture of NodeJS, .NET, Java, Ruby, and so on is a plague on software development. We&#39;re in dire need of a move toward fewer dependencies and better-vetted dependencies.</p> </blockquote></pre>burtgummer45: <pre><blockquote> <p>You fall into the fallacy described in the video as Predetermination. You have already decided that you will forever use an external router because http.ServeMux &#34;lacks features&#34;. This is not what I would call good engineering.</p> </blockquote> <p>I know I need url parameters, I need to group routes together and apply middleware differently to each group, and I want easy subrouter mounting so I can compose my app in smaller pieces and plug them together. It looks like if I used http.ServeMux I&#39;d have to reimplement this all myself, so I&#39;m going with chi.</p></pre>shovelpost: <pre><p>Sure, use whatever makes sense for your project&#39;s requirements. But saying &#34;I&#39;m never using http.ServeMux&#34; without talking about the project&#39;s requirements is not very good.</p></pre>burtgummer45: <pre><p>I doubt I&#39;ll ever use http.ServeMux, its just too limited, even crippled. A router that makes me handle params manually? You&#39;ll never convince me that&#39;s a good idea.</p></pre>shovelpost: <pre><blockquote> <p>http.ServeMux is a good example.</p> </blockquote> <p>Is it? Maybe I am alone in this but I&#39;ve been using ServeMux for years and I&#39;ve had no issues.</p></pre>burtgummer45: <pre><p>I got the impression that it cannot do parameters, doesn&#39;t do the subroute composable thing, and performs pretty bad compared to everything else, if that matters.</p></pre>dgryski: <pre><p>In all my years writing high performance services in Go I&#39;ve not once seen HTTP routing show up in my pprof output.</p></pre>losingthefight: <pre><p>I would agree. DB Access, I/O, network calls, etc tend to be far more draining on performance than a good HTTP router. I mean, I don&#39;t use the standard router, but I haven&#39;t encountered a situation in which the router was my bottleneck. </p></pre>burtgummer45: <pre><p>I don&#39;t doubt it, thats why I wrote &#34;if it matters&#34;, but don&#39;t you agree that if you want params you need to use something else, or did I miss something?</p></pre>: <pre><p>[deleted]</p></pre>umadbrobot: <pre><p>&#34;Mouth breather. &#34;</p></pre>dlsniper: <pre><p>If it matters, everything else except fasthttp is literally built on top of net/http.ServeMux.</p> <p>So... I&#39;m not sure to what you are talking about when you say it&#39;s performing pretty bad comparing to the everything else (which is built on top of it), but I&#39;m sure you&#39;ve got your facts wrong.</p> <p>The only issue with net/http is that it doesn&#39;t have a convenient way to handle the various routing other packages depend on, and you&#39;d need to build your own router every time. See an example here: <a href="https://blog.merovius.de/2017/06/18/how-not-to-use-an-http-router.html" rel="nofollow">https://blog.merovius.de/2017/06/18/how-not-to-use-an-http-router.html</a></p> <p>And to add to dgryski&#39;s user report, I too have never experienced issues with the http performance in Go, mainly because by the time a real app gets to be used, I run out of network bandwidth, memory, performance on the dependency services (which usually have the similar recursive problem), etc.</p></pre>: <pre><p>[deleted]</p></pre>pdffs: <pre><p>It&#39;s better in that it actually functions correctly, but I&#39;d argue that the <code>io.ReadWriter</code> API of <code>x/net/websocket</code> is often preferable.</p></pre>: <pre><p>[deleted]</p></pre>pdffs: <pre><p>Being able to treat a websocket like any other socket means that it can be used with any library that can work with the <code>io</code> interfaces, rather than having to build bespoke message-based code. Websocket is kind of a weird spec - it&#39;s actually a stream of frames, with message semantics layered on top, providing both APIs would be preferable IMO.</p></pre>losingthefight: <pre><p>I am primarily a web API developer and microservices systems architect, so I have a biased opinion. Two right off the bat I cannot live without:</p> <p>github.com/stretchr/testify/assert Being able to assert on various conditions makes things SO much nicer and cleaner.</p> <p>github.com/jmoiron/sqlx NamedExecs, auto-scanning, and more makes this a dream to work with. Database access has been far more elegant.</p> <p>I also love Chi, but enjoyed Gin and Gorilla.</p></pre>fourgbram: <pre><p><a href="https://www.github.com/jmoiron/sqlx" rel="nofollow">sqlx</a> has been very helpful. Without it, while fetching data from my database, I&#39;d have to make variables for each column, with the same name as the column in the DB, and then scan into those variables.</p> <p>Without sqlx:</p> <pre><code>query := &#34;SELECT username, userage, userheight FROM person&#34; rows, err := db.Query(query) //do error checking here var username, userage, userheight string for rows.Next(){ err = rows.Scan(&amp;username, &amp;userage, &amp;userheight) } </code></pre> <p>With sqlx:</p> <pre><code>type Person struct{ var name string `db:&#34;username&#34;` var age string `db:&#34;userage&#34;` var height string `db:&#34;userheight&#34;` } rows, err := db.Queryx(query) var p Person for rows.Next(){ err = rows.StructScan(&amp;p) } </code></pre> <p><code>StructScan()</code> is super helpful.</p></pre>losingthefight: <pre><p>Or even better:</p> <pre><code>type Stuff struct { ID int64 `json:&#34;id&#34; db:&#34;id&#34;` UserID int64 `json:&#34;userId&#34; db:&#34;userId&#34;` SomeField int64 `json:&#34;someField&#34; db:&#34;someField&#34;` } func GetStuff() ([]Stuff, error) { stuff := make([]Stuff, 0) err := Config.DbConn.Select(&amp;stuff, &#34;SELECT * FROM Stuff&#34;) return stuff, err } </code></pre> <p>edit: code formatting</p></pre>fourgbram: <pre><p>Wow, didn&#39;t know that one. Thank you. </p></pre>

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

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