Shared variable in package

xuanbao · · 643 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Either I forgot how to do this or I&#39;m confused. I want to parse all my template files in one file but make that available in the others, all inside the same package. So I tried this outside any function:</p> <pre><code>var Thing, err = template.ParseFiles(baseTemplate...) </code></pre> <p>Which works when I do </p> <pre><code>t := Thing </code></pre> <p>in another module inside the same package but if I try to follow that with some error handling like:</p> <pre><code>if err != nil { ... } </code></pre> <p>It fails with </p> <blockquote> <p>non-declaration statement outside function body</p> </blockquote> <p>but if I put all that inside a function, one of the other go modules can&#39;t see Thing. It&#39;s &#34;undefined&#34;. </p> <p>How can I get the other go files inside the same package to use Thing?</p> <p>EDIT: Perhaps my real question is, how do I handle err in the code above that works?</p> <hr/>**评论:**<br/><br/>FUZxxl: <pre><p>Use an <code>init</code> function to initialize these variables.</p></pre>dhdfdh: <pre><p>Example please. I&#39;ve tried so many variations.</p> <p>Here&#39;s mine:</p> <pre><code>var Thing *template.Template func SetupHandlers() { Thing, err = template.ParseFiles(baseTemplates...) } </code></pre> <p>This gives the error:</p> <blockquote> <p>undefined: err</p> </blockquote></pre>FUZxxl: <pre><p>Your variable <code>err</code> is undeclared. Try this:</p> <pre><code>func SetupHandlers() { var err error Thing, err = template.ParseFiles(baseTemplates...) } </code></pre></pre>argandas: <pre><p><a href="https://golang.org/doc/effective_go.html#init" rel="nofollow">https://golang.org/doc/effective_go.html#init</a></p> <pre><code>var Thing *template.Template func init() { var err error Thing, err = template.ParseFiles(baseTemplates...) if err != nil { panic(err) } } </code></pre></pre>dhdfdh: <pre><p>Apparently my only error is thinking err is already defined by template/Template. The examples I&#39;ve been following all use &#39;err&#39; so I made that assumption.</p> <p>Anyway, just adding &#39;var err error&#39; solved all my issues. Thanks.</p></pre>TheMerovius: <pre><p>Use <a href="http://golang.org/pkg/text/template/#Must" rel="nofollow">template.Must</a></p></pre>dhdfdh: <pre><p>That doesn&#39;t answer the question but, at the same time, it solves the problem. However, I didn&#39;t understand why I couldn&#39;t figure out what was going on with &#39;err&#39;, which was the real objective.</p> <p>EDIT: For that matter, I still don&#39;t understand why I have to define &#39;err&#39; when it&#39;s the return value for template.ParseFiles</p></pre>FUZxxl: <pre><p>In Go, you need to declare variables before you use them. In <a href="https://www.reddit.com/r/golang/comments/3hgwmd/-/cu78woy" rel="nofollow">your comment</a> you don&#39;t do so, which is why you get an error.</p></pre>dhdfdh: <pre><p>I know but the examples I&#39;ve been using don&#39;t declare it either, and they worked, which led to my confusion as to why it doesn&#39;t work now. </p></pre>trinchan: <pre><pre><code>var Thing *template.Template func SetupHandlers() { Thing, err := template.ParseFiles(baseTemplates...) if err != nil { panic(err) } } </code></pre> <p>In this case, the code will compile, but when you assign a variable using :=, like</p> <pre><code>Thing, err := template.ParseFiles(baseTemplates...) </code></pre> <p><em>both</em> Thing and err are assigned to new local variables. The package global Thing is not assigned. This is <a href="https://en.wikipedia.org/wiki/Variable_shadowing" rel="nofollow">variable shadowing</a>.</p> <p>On the other hand, assigning with the = operator, like</p> <pre><code>Thing, err = template.ParseFiles(baseTemplates...) </code></pre> <p>tries to assign to already declared variables. Thing is defined at the package level. However, err is not defined yet, so you will get a compilation error. This is why we first define err, then use the = operator to assign to the local err variable and the package level Thing,</p> <pre><code>var err error Thing, err = template.ParseFiles(baseTemplates...) </code></pre> <p>A playground exemplifying this effect is <a href="http://play.golang.org/p/wtWyBKVCjf" rel="nofollow">here</a>.</p> <p>Hopefully this clears things up!</p></pre>dhdfdh: <pre><p>Aha! <strong>That</strong> is where my mistake lies. I should have caught that myself but thanks cause I moved on and wouldn&#39;t have run into it again for who knows how long.</p></pre>

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

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