A few questions for my fellow gophers

polaris · · 549 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have finished a couple of smaller services and I&#39;ve got a couple of questions for people more experienced than myself:</p> <ul> <li>How do you structure your project? Currently I create a new folder inside $GOPATH/src/github.com/username/ and a cmd folder inside for the &#34;main.go&#34; file, which then takes a lot of time, if I want for example to add it to a different source controll (I&#39;ve been migrating my stuff over to Gitlab)</li> <li>How do you handle dependencies? So far I&#39;ve used &#34;go get&#34; and just use those, but I&#39;ve found it was not as easy to update all of them. I&#39;ve heard of govendor and similar tools, is there anything like that out-of-the-box?</li> <li>How do you handle database reference? Do you use context to pass it to the handlers, do you expose it only to the models?</li> <li>How do you make sure the server keeps running? Do you create a service (systemd) to do that?</li> <li>The templating system seems somewhat lacking to me, I&#39;ve found it to be a tedious task using include-like structure, how do you handle that?</li> </ul> <hr/>**评论:**<br/><br/>Bake_Jailey: <pre><ul> <li><p>Same, though you&#39;d have to use a directory inside a cmd directory to make sure that the binary ends up with the right name.</p></li> <li><p>I use govendor. glide is also good, and some use godep. There&#39;s also dep, which is a work in progress might be official tool. There&#39;s gb, which gives each project its own gopath, though I prefer the more standard vendoring method.</p></li> <li><p>Only use contexts for request scoped values. Database references should be passed in elsewhere. My go-to is to define handlers on a struct which contains a reference to the handle (which may or may not be viable depending on what you&#39;re doing, it&#39;s very natural for me since I use chi for my routing). You could do a global variable, but that feels wrong.</p></li> <li><p>You could use a systemd service, yes, or something more complicated like orchestration. I&#39;m lazy, so I use dokku on a server and have it use the heroku buildpack for Go. Lots of options.</p></li> <li><p>There are numerous libraries out there to make things easier. There&#39;s unrolled/render if you want something that&#39;s based on standard library templates. You could use pango2, which is very close to Django templates. I like quicktemplate, which uses code gen to produce templates that look like Go code. The awesome-go page has a big list of template libraries and tools.</p></li> </ul> <p>A quick Google should net you what I&#39;m not directly linking (on my phone...).</p></pre>sh41: <pre><blockquote> <p>How do you handle dependencies? So far I&#39;ve used &#34;go get&#34; and just use those, but I&#39;ve found it was not as easy to update all of them.</p> </blockquote> <p>I&#39;m curious what you would think of <a href="https://github.com/shurcooL/Go-Package-Store" rel="nofollow">Go Package Store</a> and whether you think it makes updating Go packages you rely on easier.</p></pre>ar1819: <pre><ol> <li>cmd/project_name if project is big enough and I know that internals can be used somewhere else. In simple projects everything goes in top level directory (like 3 to 7 files simple) </li> <li>trash/vndr. They are the only ones who do their job reliably on our dev and prod environments. They are also quite simple to use . YMMV of course. </li> <li>DI through New function or directly during struct initialization. Do not use context.Value for that - only use context for request based data (headers, requestId and so on). Cancelation of app is entirely different story tho... </li> <li>Docker? There is myriad of ways to do this, so I&#39;m not sure that there is any &#34;proper&#34; way. </li> <li>I don&#39;t use templates that often so the only thing I can recommend is - look at the other options, maybe? Dunno about their code quality so be careful when with them.</li> </ol></pre>

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

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