Switching from node.js to go, point me in the right direction

xuanbao · · 861 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So I&#39;ve decided to dip my toes in the Go pool and I find it extremely fun and refreshing (coming from a dynamic background, who knew static typing could be so useful).</p> <p>I will primarily use Go for api servers and there are a few questions I have:</p> <p>1) Go obviously doesn&#39;t have an event loop, so how does a Go server handle multiple requests at the same time? I&#39;ve read about goroutines but all the different &#34;web framework&#34; hello world examples I&#39;ve seen don&#39;t seem to make use of them.</p> <p>2) How can I setup automatic recompilation on file changes? Something like &#34;go watch&#34; doesn&#39;t seem to exist.</p> <p>3) Any articles with examples comparing Go code and node code doing the same thing? I&#39;ve read most of the &#34;Resources for new Go programmers&#34; from the sidebar.</p> <p>Thank you for reading. :)</p> <hr/>**评论:**<br/><br/>peterhellberg: <pre><p>2: You can use gin to do code reloading on change: <a href="https://github.com/codegangsta/gin">https://github.com/codegangsta/gin</a></p></pre>torwori: <pre><p>This looks like what I thought I needed, thanks!</p></pre>peterhellberg: <pre><p>You are welcome.</p> <p>Also, my favorite tool to run tests when a file in the project change: <a href="https://github.com/jpillora/spy/" rel="nofollow">https://github.com/jpillora/spy/</a></p></pre>j1436go: <pre><p>In answer to 2) Godo is a pretty good task runner: <a href="https://github.com/go-godo/godo">https://github.com/go-godo/godo</a> A makefile with <code>watch make ...</code> works also pretty well.</p></pre>EclecticMind: <pre><p>Comparisons between Node.js and Go are plenty but you need to specify what you&#39;re looking for. An article about someone&#39;s success story, or the event handling technical differences. </p> <p>Like others mentioned, every http request runs independent of one another. Think of them as asynchronous, except they look like regular functions and you don&#39;t even need to pass a callback! Isn&#39;t it nice?</p> <p>Recompiling files on the fly isn&#39;t hard, but which files are you talking about? Go, or templates and such? I would check out reflex. Gin became a pain in the ass to deal as I created more projects. Gin is basically a proxy to your app, whereas reflex just reruns the command you give it when you change a file that&#39;s being watched. Think of gulp or grunt file watching then running a task. </p> <p>Simplify your life by not worrying about asynchrony and speed for now. Goroutines are simple, I would add them in after you figured out the other stuff. </p></pre>torwori: <pre><p>I&#39;d like to see both successes and differences.</p> <p>When you put it that way, yeah, it looks awesome!</p> <p>Reflex/gin both seem to me like the thing I was looking for.</p> <p>Thanks for the advice! :)</p></pre>tagesticket: <pre><p>1) I would rephrase this as Go doesn&#39;t expose the event loop directly. The event loop exists in Go, but is managed by the runtime. A simplified view of how it works is the following:</p> <p>You write all your code as blocking synchronous code which runs in execution contexts called goroutines. These are similar to threads with the primary difference being that they are managed by the go runtime and not the OS. The runtime can detect when your code is at a blocking call boundary such as a read or write. If it knows you&#39;ll be waiting for the result of some operation it runs code from another goroutine until that piece of code blocks.</p></pre>torwori: <pre><p>So essentially, I can think of (in my case) every request spawning a goroutine which does it&#39;s own thing, all the while being completely non-blocking (one goroutine waiting on a db call or read/write will mean nothing for the others). Is this right?</p></pre>tagesticket: <pre><p>Exactly. If you&#39;re using the net/http library then the server does exactly this. You provide it with a handler that is called for every request. Each request is then processed in its own goroutine by the handler. There are some catches to be aware of though. All the magic the runtime does won&#39;t work if you call into a C library. In that case the call will be fully blocking. An example would be if you had accesses to a sqlite database.</p></pre>torwori: <pre><p>Good to know, thanks!</p></pre>pierrrre: <pre><p>1) a simple HTTP server: <a href="http://golang.org/pkg/net/http/#ListenAndServe" rel="nofollow">http://golang.org/pkg/net/http/#ListenAndServe</a> It creates a new goroutine (lightweight thread) for each HTTP request.</p> <p>2) Press &#34;ctrl+C&#34; and run your program again</p> <p>3) <a href="https://gobyexample.com/" rel="nofollow">https://gobyexample.com/</a> <a href="https://golang.org/doc/effective_go.html" rel="nofollow">https://golang.org/doc/effective_go.html</a></p></pre>idiomatic: <pre><blockquote> <p>2) Press ctrl-C...</p> </blockquote> <p><a href="https://github.com/idiomatic/seppuku">or have code that watches for file change and automatically exits</a>.</p></pre>ecmdome: <pre><p>Your username &gt;</p></pre>jerf: <pre><blockquote> <p>Go obviously doesn&#39;t have an event loop, so how does a Go server handle multiple requests at the same time?</p> </blockquote> <p>Go doesn&#39;t &#34;have&#34; an event loop, Go <em>is</em> an event loop. In the way you know the word, all Go code is always asynchronous, all the time.</p> <blockquote> <p>I&#39;ve read about goroutines but all the different &#34;web framework&#34; hello world examples I&#39;ve seen don&#39;t seem to make use of them.</p> </blockquote> <p>net/http is automatically spawning one goroutine per request, so you don&#39;t see it. They&#39;re still there and you can still use them freely.</p> <p>The other two I can&#39;t really cover. I&#39;m not a huge fan of automatic recompilation on change, but I freely acknowledge that&#39;s just a taste thing and there&#39;s nothing wrong with it. I&#39;m more into compilation errors showing up in my editor. Perhaps if you mention and/or Google, you can find out how to turn that on in your editor, as it&#39;s useful either way.</p></pre>jerf: <pre><p>I have to admit I&#39;m at a loss as to what&#39;s so horrible about this post. It really is the best way to explain Go to a Node user.</p></pre>torwori: <pre><blockquote> <p>net/http is automatically spawning one goroutine per request, so you don&#39;t see it. They&#39;re still there and you can still use them freely.</p> </blockquote> <p>This is the explanation I was looking for, thanks!</p></pre>

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

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