<p>I'm experimenting with breaking a complex web app into reusable components, as packages. One requirements is that these packages must have their own templates directory.</p>
<p>The problem is that it seems there is no way in go to find out the directory of a package, so I cannot walk through the templates directory of that package, and parse each file.</p>
<ol>
<li>Is getting the directory of a package in go really impossible?</li>
<li>What are some workarounds for parsing template files in packages?</li>
</ol>
<hr/>**评论:**<br/><br/>Loves_Portishead: <pre><p>You should look at using something like go-bindata, in conjunction with //go:generate (sorry, on mobile can'tllink the docs) to generate a virtual filesystem (see go-bindata) asset directory which is compiled into your resulting binary. As others have said, there's nothing to suggest that your binary will have any access to the sources in the GOPATH at run time, be that because its in a /tmp dir because of "go run ...", or because you copied it to a machine without a go toolchain.</p></pre>Raiyni: <pre><p>Can you not just pass the directory location to the package?</p></pre>m3wm3wm3wm: <pre><p>That would depend on the package location. It is manual, and not portable.</p></pre>Raiyni: <pre><p>I have no idea what your problem is. If you just pass the absolute path to the package, you have no problems. </p></pre>mmtiller: <pre><p>Presumably there is some reason I'm not aware of which prevents you from just doing this?</p>
<p>path.Join(os.Getenv(GOPATH), "src", pkgname)?</p></pre>FUZxxl: <pre><p><code>GOPATH</code> might not be available at runtime, which is also the reason why it doesn't make sense to have the package directory available at runtime.</p></pre>jerf: <pre><p>GOPATH is a colon-delimited list of directories, not a single directory.</p></pre>TwilightTwinkie: <pre><p>Is it really? Like you can have multiple directories that will be searched? I was not aware of this and really want to know the answer. </p></pre>SingularityNow: <pre><p>Yes, exactly that.</p></pre>TwilightTwinkie: <pre><p>That is just about the greatest news I've heard all day. Is that stated some place in the docs and I just missed it? </p></pre>SingularityNow: <pre><p>I don't know if this is the only source but
<a href="https://code.google.com/p/go-wiki/wiki/GOPATH" rel="nofollow">https://code.google.com/p/go-wiki/wiki/GOPATH</a></p></pre>jerf: <pre><p>It wouldn't really be considered a killer in Go to have to specify the directory.</p>
<p>You may also want to look into whether <code>go generate</code> can do what you're trying to do. I haven't used it for anything yet but I bet the "current directory" at generate time is the package having generate run on it.</p></pre>cs-guy: <pre><p>At what scope and time do you need the package path?</p>
<ul>
<li>Build time?</li>
<li>Run time?</li>
<li>From within the package?</li>
<li>From outside the package?</li>
<li>On your development box?</li>
<li>On a build server?</li>
<li>Where your binary is deployed?</li>
</ul></pre>m3wm3wm3wm: <pre><ul>
<li>Run time.</li>
<li>From within the package</li>
<li>On everywhere, it must be portable.</li>
</ul></pre>troy_k: <pre><p>I have been using runtime.Caller with success on the unix's; not sure about windows though... <a href="http://andrewbrookins.com/tech/golang-get-directory-of-the-current-file/" rel="nofollow">http://andrewbrookins.com/tech/golang-get-directory-of-the-current-file/</a></p></pre>NerfDawg: <pre><p>You are absolutely missing the point that <a href="/u/Loves_Portishead" rel="nofollow">/u/Loves_Portishead</a> makes below, when the binary is compiled there's no guarantee that the original directory even exists anymore, or that the binary hasn't been moved, or even that the host system has Go installed, and certainly no guarantee that your dependent <em>packages</em> are still present. Whatever you are trying to do, please stahp and learn from someone who's trying to help. The only way to make it portable is to pack the parts of the filesystem you need into a virtual filesystem <strong><em>in the binary</em></strong>.</p></pre>
Is it impossible to get the directory of a package? Or: How to parse template files in a package?
xuanbao · · 765 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传