<p>I'm new to Go, and considering dropping python for it on my upcoming project. I'm getting a bit confused trying to place the different components, and would appreciate some help figuring it out.</p>
<p>On benchmarks like <a href="https://www.techempower.com/benchmarks/#section=data-r13&hw=ph&test=db&l=4ft5hb">https://www.techempower.com/benchmarks/#section=data-r13&hw=ph&test=db&l=4ft5hb</a>, the 'top' go framework tends to flip between echo-prefork and fashttp-<somedb>-prefork. I also hear people say that net/http is perfectly fast and fine.</p>
<p>Is net/http something that's built into golang by the original developers? Is it enough to build the whole site from? </p>
<p>What do echo/fasthttp do that net/http doesn't do? </p>
<p>Are there other frameworks that do a much better job of helping you build a website instead of just looking fast on benchmarks?</p>
<p>edit: Thanks for all the answers! The general gist I get is to not worry about frameworks yet and just go with net/http until it actually becomes an problem, which sounds like a good idea. </p>
<hr/>**评论:**<br/><br/>bear1728: <pre><p>Yes, net/http is part of the standard library and was mainly written by Go devs. I don't know the exact history, but it's very possible there have been commits from people outside of Google as well.</p>
<p>My opinion on benchmarks: unless you're running a very large site with a lot of visitors, or are extremely constrained in terms of resources, I don't think it's going to make a difference. Especially if your site uses a database like sqlite or postgres/mysql/etc. One reason I like Go is that the built in testing framework is pretty easy to use and test very small and different parts of a system. I just wish you could benchmark on the playground.</p>
<p>You could build a great site without using other libraries/frameworks. But those other options do offer some helpful things. I started with <a href="https://github.com/zenazn/goji">goji</a> because it had the most example code that I could copy and paste. Now I use <a href="https://github.com/pressly/chi">chi</a>. The only real reason I use any framework is that I'm too lazy to write a function to do dynamic routes, like "/user/:id". Most frameworks/libraries I've seen have ways to do this.</p></pre>carsncode: <pre><p>So does net/http if it's just a suffix. If you use a route with a trailing slash in the stock mux, anything starting with that path will go to that handler. So /user/ route with a getUser handler would send a request for /user/bear1738 to that handler, and you can easily grab the relevant part of the path.</p></pre>bear1728: <pre><p>Good point! I totally forgot to mention that.</p></pre>ZetaHunter: <pre><p>+1 for Chi, using it in several of my own pet projects currently.</p></pre>Hunterbunter: <pre><p>Thanks, I think I'll go for a net/http start and look into the features of the different frameworks a bit more. I may have been focusing too much on speed.</p></pre>comradeswitch: <pre><p>This is what I recommend and what I do, personally. It's tempting to go for the fastest or the most feature rich framework, most of the the time, your http.Server or Client implementation is not going to be a bottleneck. Premature optimization might not be the root of all evil but it can definitely lead to needless complexity and dependencies.</p>
<p>Even if your http api is a bottleneck, there's almost always something that can be done in terms of design that will have a bigger impact. You can leave the serving of static files to something like nginx. A change to your api might allow for clients to make a few larger requests and/or cache response data. Templates can be pre-rendered partially or completely.</p>
<p>But in any case, before you start thinking about that, profiling is the best first step.</p></pre>Veonik: <pre><p>I would recommend you not put the cart before the horse. Get a feel for the language, maybe decide to use a framework like echo, maybe not. Go tends to have a very rich standard library (of which <code>net/http</code> is a part of) but there is of course room for improvement, especially with a specific use-case in mind.</p>
<p>Picking a library because it can serve a billion requests per second is perhaps the wrong approach-- are you likely to ever max out throughput to where these microbenchmarks matter?</p>
<p>Better questions might be: What features does it have? Does it make developers more productive? Does it solve your problems? I see you're already thinking along these lines, so that's great.</p>
<p>All that said, <code>net/http</code> is great and will very likely serve you well, especially starting out.</p></pre>Hunterbunter: <pre><p>I do expect concurrent use, and some of them will randomly be causing a lot of work to be done. I want to try to avoid slow down for everyone else while that's happening.</p>
<p>As long as net/http does everything the frameworks do, I'm happy giving that a go first.</p></pre>puffybunion: <pre><p>Don't worry about it. You're starting out, net/http is totally fine.</p></pre>drvd: <pre><blockquote>
<p>What do echo/fasthttp do that net/http doesn't do?</p>
</blockquote>
<p>For fasthttp: Please note that fasthttpd does <strong>not</strong> implement full HTTP! It provides a strict subset of HTTP which often is enough for simple things and serving web browsers.</p>
<p>And please do not look at such sort of "benchmarks" unless you are running Twitter or some realtime stock trading company.</p></pre>codesenberg: <pre><p>I'd like to add that not only <strong>fasthttp</strong> doesn't follows all of the RFCs related to HTTP/1.1, but it also doesn't support HTTP/2.0.</p>
<p>Still it's pretty useful, really fast and successfully used in production by <strong>VertaMedia</strong> serving up to 200K rps from more than 1.5M concurrent keep-alive connections per physical server( as cited in <a href="https://github.com/valyala/fasthttp/blob/master/README.md#fasthttp" rel="nofollow">README</a> ).</p></pre>Hunterbunter: <pre><p>Ah I didn't know that, thanks.</p>
<p>The project I'm going to build could be required to occasionally and dynamically do a lot of processing, so speed is kind of important. I wouldn't want it to grind to a halt randomly for clients. I was even considering C++ but development time is important too. Go's simplicity and speed is a big attractive thing right now.</p>
<p>Otherwise I probably would have just stuck to python for the MVP since I know it a lot better.</p></pre>BraveNewCurrency: <pre><p>Note that you are talking between 100K to 200K requests per second. It's unlikely to make a real-world difference for several reasons:</p>
<ul>
<li>Your clients won't notice the difference between 0.01ms and 0.005ms. It will literally be lost in the noise of packet jitter.</li>
<li>"But I can save money by running on half the boxes" I heard you say. Nope. In the short-term, you are not going to have 100K requests/second hitting your site, so your point is moot. By the time you DO get 100K rps (i.e. millions of users), it is likely you will be able to afford 2 boxes.</li>
<li>Yes, 100K vs 200K is a 2x speed up. But if you start actually doing any work in your app (auth, etc), then your seedup will be a lot less than 2x. In fact, for many applications (such as resizing a jpeg), the contribution of the framework will be almost too small to measure.</li>
</ul></pre>Hunterbunter: <pre><p>Would google/seo notice the speed difference and rank pages higher because of it? </p>
<p>I know that might be a funny question but I've heard that all things being equal the faster page gets the higher rank. I'm not sure if there's a cutoff.</p></pre>BraveNewCurrency: <pre><blockquote>
<p>Would google/seo notice the speed difference and rank pages higher because of it?</p>
</blockquote>
<p>Ha ha, really doubtful -- The speed rank was added because Humans don't like to wait for slow pages. Anything under 50ms is probably ranked the same. (See <a href="http://www.thesempost.com/how-page-speed-as-a-ranking-factor-works/" rel="nofollow">actual slide</a> from google presentation)</p>
<p>Also, when you are talking about 0.01ms, the jitter from all the routers in between you and the server WILL dominate your measurements anyway.</p>
<p>Just like the old joke "I don't have to be faster than the bear, I only need to be faster than you", speed doesn't matter unless it serves a specific business purpose. If you spend time speeding up something users don't care about (or can't even measure), you've wasted your time. Work on something user-visible to get the biggest bang for your buck.</p></pre>Hunterbunter: <pre><p>good advice, thanks</p></pre>jns111: <pre><p>net/http is perfect for whatever you want to build. It comes with http2 push support and websockets. On top of that you will have compatibility if you follow strictly the http.Handler pattern. But what about performance? Yes it's true, fasthttp and other frameworks/libraries achieve a lot more 200 OK responses, but what does it mean? Are you offering a ping service? In this case you should really consider one of these frameworks. But if for example you offer an api in most cases you will burn cpu and ram to marshal and unmarshal data, process stuff and wait for a database to answer your query. So in a realistic scenario you will not benefit much from using such fast library because your bottleneck will be other parts like the database.</p></pre>Hunterbunter: <pre><p>Thanks for your response, it's another +1 for net/http. The benchmarks I put in OP does supposedly test things like json creation, single/multi queries, random fortune cookie thing, and so on. </p></pre>smcquay: <pre><p>I have been writing Go professionally for three years. Services I have written have served billions upon billions of requests. I use net/http. </p></pre>Hunterbunter: <pre><p>cool an extra thumb up for net/http.</p></pre>itsmontoya: <pre><p>Just stick with stdlib. I've done quite a bit of real world testing with the other libraries and didn't see any performance gains (in fact, I saw decreases when comparing against stdlib)</p></pre>
What's the difference between net/http and all the other things (eg echo, fasthttp, etc) for web development?
agolangf · · 843 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传