<p>I have finished a couple of smaller services and I'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 "main.go" file, which then takes a lot of time, if I want for example to add it to a different source controll (I've been migrating my stuff over to Gitlab)</li>
<li>How do you handle dependencies? So far I've used "go get" and just use those, but I've found it was not as easy to update all of them. I'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'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'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's also dep, which is a work in progress might be official tool. There'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're doing, it'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'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's unrolled/render if you want something that'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'm not directly linking (on my phone...).</p></pre>sh41: <pre><blockquote>
<p>How do you handle dependencies? So far I've used "go get" and just use those, but I've found it was not as easy to update all of them.</p>
</blockquote>
<p>I'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'm not sure that there is any "proper" way. </li>
<li>I don'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传