When browsing through some go code, I saw a variable declaration saying: _ = fmt.Printf - What does this do?

xuanbao · · 460 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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&#39;t use.. </p></pre>fortytw2: <pre><p>it&#39;s used in autogenerated code a lot - not really sure of the intention though</p></pre>ctbel: <pre><p>That&#39;s actually a very good observation.</p> <p>One particular situation which immediately comes to mind is code generation that might use, but isn&#39;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 &#34;goimports&#34; and without having to manually manage package imports.</p> <p>TL;DR: laziness</p></pre>driusan: <pre><p>That&#39;s why I said in this context. (I looked at the code, and I didn&#39;t see any notice that it was autogenerated, so I assumed it wasn&#39;t.)</p> <p>In the context of autogenerated code, where code is held to a lower standard, it makes sense because you&#39;re often not really sure if it&#39;s going to be used in the generated portion of the code or not.</p></pre>Uncaffeinated: <pre><p>Perhaps they don&#39;t have auto-goimports set up. Or don&#39;t feel like introducing extra churn in diffs.</p></pre>driusan: <pre><p>I don&#39;t use auto-goimports either, but I don&#39;t have much problem replacing &#39;&#34;fmt&#34;&#39; with &#39;// &#34;fmt&#34;&#39; when the compiler complains, and removing the &#39;//&#39; when I want it back..</p></pre>bannerad: <pre><p>Ok. I&#39;ll bite. Why would one do this? What value does &#34;fmt&#34; 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&#39;t remember that when I needed it :P Thanks! </p></pre>3264128256: <pre><p>Also press &#39;y&#39; to get a permanent link to file/line.</p></pre>

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

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