<p>Hi all,</p>
<p>I'm working on my first project in Go. It's a simple wallboard server with two HTML pages and a WebSocket endpoint. Currently, for the HTML pages, I have a file called <code>templates.go</code> where I put the heredoc'd templates.</p>
<p>Loading the template files directly doesn't get the result I want because as soon as you run the binary from anywhere other than the package's directory, it's fatals for days.</p>
<p>Not a huge fan of the heredoc approach either though: A bunch of HTML, and JS thrown into some strings. Doesn't feel very clean, nor does it exactly make development pleasant to either be editing w/o any HTML/JS support from my editor, or to write it in a separate file and have to copy it over every time.</p>
<p>Has anyone found a better solution?</p>
<hr/>**评论:**<br/><br/>q1t: <pre><p>Hi, I guess you could start with this <a href="https://github.com/jteeuwen/go-bindata">https://github.com/jteeuwen/go-bindata</a>
and there are other similar tools for this job</p></pre>dwevlo: <pre><p>You can also use this in combination with <code>go:generate</code>:</p>
<pre><code>package main
import "log"
//go:generate go-bindata -nomemcopy data/...
func main() {
bs, err := Asset("data/template.gohtml")
log.Println(bs, err)
}
</code></pre>
<p>When you run <code>go generate</code> it will create the <code>bindata.go</code> file.</p>
<p>In general though I either archive the whole app into a .tar.gz, or rsync a folder to the server, so I don't find I need everything stuffed into the binary. (but I'm also making web apps...)</p>
<p>For an installed application you'd typically have some well-defined location to store your files. On install you'd extract everything to that location.</p>
<p>If you need it go-gettable then you might get away with some GOPATH trickery (get it with <code>os.Getenv("GOPATH")</code> and then <code>filepath.Join(gopath, "your/library/path/data/template.gohtml</code>). That's probably not super reliable though.</p></pre>elithrar_: <pre><blockquote>
<p>For an installed application you'd typically have some well-defined location to store your files. On install you'd extract everything to that location.</p>
</blockquote>
<p>Or pass in the path as an env var, flag or config file. Can fall back to a default "same directory" as well for simplicity during testing.</p></pre>foo-bar-qux: <pre><p>Yup I've been using go-bindata. It works well. If you use IntelliJ, be sure to use the File Watchers plugin to run it whenever your templates change.</p></pre>singron: <pre><p>I would make a package that can be tracked by your package manager (deb, rpm, etc.). It makes a lot of things much easier (installing, uninstalling, upgrading, downgrading, etc.). Just put the files in a directory that makes sense. In your service script, you can give an argument or environment variable to your program telling it where to find its files (or just cd to there).</p>
<p>You can also install multiple versions simultaneously (for zero downtime upgrades and rollbacks), although the details of this vary, and you have to construct your packages so that different versions won't conflict. The good news is that the package manager is checking your work for you.</p></pre>gchain: <pre><p>There are several projects that help you with embed files into your project:</p>
<p><a href="https://github.com/gchaincl/gotic" rel="nofollow">https://github.com/gchaincl/gotic</a></p>
<p><a href="https://github.com/GeertJohan/go.rice" rel="nofollow">https://github.com/GeertJohan/go.rice</a></p>
<p><a href="https://github.com/jteeuwen/go-bindata" rel="nofollow">https://github.com/jteeuwen/go-bindata</a></p></pre>tvmaly: <pre><p>gorazor is another option <a href="https://github.com/sipin/gorazor" rel="nofollow">https://github.com/sipin/gorazor</a> it uses code generation for your templates, so they effectively become go code.</p></pre>Mteigers: <pre><p>You could use flags or env variables. We use env variables in production for specifying template directories. </p></pre>klaaax: <pre><p>deal with it like any other program, your app needs to use an environment variable that holds the path to the assets.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传