<p>Line 23 in <a href="https://github.com/papertrail/remote_syslog2/blob/master/remote_syslog.go" rel="nofollow">https://github.com/papertrail/remote_syslog2/blob/master/remote_syslog.go</a></p>
<p>Thanks! </p>
<hr/>**评论:**<br/><br/>idkwtfiad: <pre><p>It prevents the Go compiler from giving an error about unused imports. </p></pre>driusan: <pre><p>That seems silly in a context where you could get rid of the compile error about unused imports by just not importing things that you don't use.. </p></pre>fortytw2: <pre><p>it's used in autogenerated code a lot - not really sure of the intention though</p></pre>ctbel: <pre><p>That's actually a very good observation.</p>
<p>One particular situation which immediately comes to mind is code generation that might use, but isn't guaranteed to use, certain packages. Take for example a code generator that allows a --verbose flag which inserts logging messages using the fmt package.</p>
<p>It takes 30 seconds to modify the static template to include the fmt package as well as the hack described above. The generated code will now compile without having to run the output through "goimports" and without having to manually manage package imports.</p>
<p>TL;DR: laziness</p></pre>driusan: <pre><p>That's why I said in this context. (I looked at the code, and I didn't see any notice that it was autogenerated, so I assumed it wasn't.)</p>
<p>In the context of autogenerated code, where code is held to a lower standard, it makes sense because you're often not really sure if it's going to be used in the generated portion of the code or not.</p></pre>Uncaffeinated: <pre><p>Perhaps they don't have auto-goimports set up. Or don't feel like introducing extra churn in diffs.</p></pre>driusan: <pre><p>I don't use auto-goimports either, but I don't have much problem replacing '"fmt"' with '// "fmt"' when the compiler complains, and removing the '//' when I want it back..</p></pre>bannerad: <pre><p>Ok. I'll bite. Why would one do this? What value does "fmt" have to the rest of that module?</p></pre>idkwtfiad: <pre><p>I can only surmise it was used to quickly add/remove debug output without having to change the import statement (this is rather redundant if you use goimports or a similar tool, but not everyone does).</p></pre>arulprasad: <pre><p>That makes sense. Thanks!</p></pre>theGeekPirate: <pre><p>FWIW, you can click on the line number to create a <a href="https://github.com/papertrail/remote_syslog2/blob/master/remote_syslog.go#L23" rel="nofollow">direct link</a>.</p></pre>arulprasad: <pre><p>Right! Didn't remember that when I needed it :P Thanks! </p></pre>3264128256: <pre><p>Also press 'y' to get a permanent link to file/line.</p></pre>
When browsing through some go code, I saw a variable declaration saying: _ = fmt.Printf - What does this do?
xuanbao · · 460 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传