<p>I want to avoid global variables in my tests, would it be better to make a function that retuns a local variable for use in a test or just make a global variables in the test because it wont be compiled into the main program (or will it?)</p>
<p>example of using a function to return a local variable</p>
<p><code>
func DefaultConfig() *Config {
return &Config{
SomeValue: 1,
OtherValue: 2,
}
}
</code></p>
<p>Or since it isnt compiled just use gloabl test variable</p>
<p><code>
var DefaulttestConfig = &config{
Somevalue: 1,
Someothervalue: 2,
}
</code></p>
<hr/>**评论:**<br/><br/>edwinTop: <pre><p>Ben johnson wrote a helpful post - check out point 1
<a href="https://medium.com/@benbjohnson/structuring-applications-in-go-3b04be4ff091" rel="nofollow">https://medium.com/@benbjohnson/structuring-applications-in-go-3b04be4ff091</a></p></pre>0xjnml: <pre><p>Nit: Go doesn't have global scope. Somehow similar is the universe scope, but user code cannot define anything in universe scope. Go package scope is what you probably mean by 'global scope'.</p></pre>jackmott2: <pre><p>Just as an aside, if you ever find yourself having to create a bit of a mess, or add a bunch of code, to avoid one of the rules of thumb of programming, you might consider just breaking the rule.</p>
<p>I don't know if this is one of those cases, just keep that in mind.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传