Including non-source code files in a Go program

xuanbao · · 595 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to make a website using Go. I have a server program, but obviously the source code for that is not the only important part. I want to include the source code for the front-end in the same repository, but I&#39;m unsure of how to handle this.</p> <p>My structure looks kind of like this</p> <pre><code>go (my gopath) src github.com rivertam serverproject files.go subproject1 subproject1.go subproject2 subproject2.go </code></pre> <p>I feel like I shouldn&#39;t be building it inside the project directory because I don&#39;t want it included in git... or do I?</p> <p>Anyways, basically I need a place to put my templates that will both be in the repository and accessible by the executable. Super ideally, I would love it if something like <code>go build</code> actually output a full directory with an executable and the relevant templates and stuff. Any advice?</p> <hr/>**评论:**<br/><br/>ikofai: <pre><p>I&#39;ve used this with great success in the past for packing along static assets, etc. </p> <p><a href="https://github.com/jteeuwen/go-bindata" rel="nofollow">https://github.com/jteeuwen/go-bindata</a></p> <p>It was used for bundling the pre-packaged views with authboss (via go generate) </p> <p><a href="https://github.com/go-authboss/authboss/tree/master/internal/response" rel="nofollow">https://github.com/go-authboss/authboss/tree/master/internal/response</a></p></pre>gkx: <pre><p>Thanks! This looks interesting to say the least. I&#39;ll have to look further into it when I get the chance. </p></pre>ikofai: <pre><p>No problem. If you look into it we actually allow overriding of the packaged assets as well by specifying a directory with the files. </p> <p>If you have any questions feel free to message me. </p></pre>TrueFurby: <pre><p>This might help you with that: <a href="https://github.com/GeertJohan/go.rice" rel="nofollow">https://github.com/GeertJohan/go.rice</a></p></pre>q1t: <pre><p>It mostly depends on what kind of the project is it. You can put your files like templates, assets, etc in the same directory to access them easly in your server code, but subprojects, for example if they are only (designed) for that serverproject, I&#39;d rather put them in serverproject directory, then somewhere else :) if you feel like these projects are more common and you will reuse them later, I&#39;d put them in separate paths. However, it&#39;s all just about paths, whenever you want you can move them out of serverproject or in.</p> <p>In my experience subprojects are often just models for a database. And what do you mean by front-end code? Are you making an api server and want to build a website(server) based on that? I would then make them in separate directories.</p> <p>go build works only in the scope of a package, it does not know about any files (images, js, css, templates) and directories else. </p> <p>&lt;standart excuse for poor English grammar&gt;</p></pre>gkx: <pre><p>I&#39;m not sure you&#39;re understanding what I&#39;m saying, so I&#39;ll simplify.</p> <p>Let&#39;s say I want to make a static html server. Obviously, the Go code would be pretty trivial. In my serverproject directory, I have main.go. When I do a <code>go build github.com/username/serverproject</code>, I get an executable in my current directory.</p> <p>Because I have to specify a path in the code (relative or absolute), I need to know where the files are. I probably shouldn&#39;t assume anyone with the executable has the assets at <code>/home/name/go/src/github.com/username/serverproject/assets</code>, so I probably want to always output (with go build or maybe something else) a directory with the executable at the root and the assets inside a subdirectory... But I&#39;m not sure if that&#39;s good practice and even if it is, I&#39;m wondering if there&#39;s a way to do that. I could use something like gulp or a makefile to do it, but I think that isn&#39;t really the &#34;Go Way&#34; </p></pre>klaaax: <pre><p>just allow people to override the asset path with a command line argument, and default to the working directory.</p></pre>sh41: <pre><p>I think including your HTML templates in the git repo is a good idea. Maybe under assets sub folder.</p></pre>

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

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