Is it impossible to get the directory of a package? Or: How to parse template files in a package?

xuanbao · · 748 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;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&#39;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&#39;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 &#34;go run ...&#34;, 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&#39;m not aware of which prevents you from just doing this?</p> <p>path.Join(os.Getenv(GOPATH), &#34;src&#34;, pkgname)?</p></pre>FUZxxl: <pre><p><code>GOPATH</code> might not be available at runtime, which is also the reason why it doesn&#39;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&#39;ve heard all day. Is that stated some place in the docs and I just missed it? </p></pre>SingularityNow: <pre><p>I don&#39;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&#39;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&#39;re trying to do. I haven&#39;t used it for anything yet but I bet the &#34;current directory&#34; 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&#39;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&#39;s no guarantee that the original directory even exists anymore, or that the binary hasn&#39;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&#39;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>

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

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