Noob: VSCode is deleting my import line

polaris · · 618 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Since I&#39;m new, I&#39;m sure there is a good reason for this, but this is really ticking me off. Whenever I have an unused import line and save the file, it deletes my import line. I am currently trying things out, commenting code out and back in, and it keeps deleting my imports when I do. (And doesn&#39;t have the decency to put it back in when I uncomment the code.)</p> <p>How do I make it stop deleting my stuff?</p> <pre><code>package main import ( &#34;fmt&#34; &#34;log&#34; &#34;os&#34; &#34;github.com/dgryski/go-bitstream&#34; ) </code></pre> <p>When I comment out code referring to <code>go-bitstream</code>, that list line disappears when I save. &gt;:(</p> <p>Maybe this should go into a different subreddit, but the culprits seem to be Go programs being run whenever I save.</p> <p>Edit: Apparently it will add the correct import as long as I get the package name correct. I was trying to use <code>go-bitstream.NewReader()</code> when <code>bitstream.NewReader()</code> is what I should have been using. Thanks to <a href="/u/TheMerovius" rel="nofollow">/u/TheMerovius</a> and all responders!</p> <hr/>**评论:**<br/><br/>szabba: <pre><p>It&#39;s a compiler error in Go to have an unused import. It&#39;s possible the default config of VSCode&#39;s Go plugin autoimports the packages that are being used and fixes the import list along with autoformatting the code. If that&#39;s the case you should see &#34;fmt&#34; added to the import list after you first use it in an empty program.</p></pre>midnightFreddie: <pre><p>It did indeed add &#34;fmt&#34; when I tried a basic &#34;hello, world&#34; without explicitly importing.</p> <p>I don&#39;t suppose you know offhand if there is a way I could make it recognize where to find <code>go-bitstream</code> automatically?</p></pre>TheMerovius: <pre><p>It actually should be able to figure that out already, if your GOPATH is configured correctly, as long as you a) use the correct package name (for that particular package it&#39;s &#34;bitstream&#34;, as evident from the header <a href="https://godoc.org/github.com/dgryski/go-bitstream" rel="nofollow">here</a> and b) the way you use it is correct and sufficiently unique (i.e. the &#34;mapping (pkg-name, types you use) → package&#34; must be well-defined).</p> <p>The idea behind goimports is, that you don&#39;t have to really worry about which imports you use anymore; just write the code and let it figure out the import lines for you (and re: you&#39;d much prefer… - give it some time. You&#39;ll learn to <em>love</em> not worrying about imports). I&#39;d say in 99% of cases that works perfectly well - even in a large GOPATH like Google&#39;s :)</p> <p>If it really doesn&#39;t work for you and you get annoyed, you can add a <code>var _ *bitstream.BitReader</code> at the package scope during experimenting, which counts as a usage and will make goimports not delete the import anymore. But really, you should figure out how to make it work to your advantage :)</p></pre>midnightFreddie: <pre><blockquote> <p>as long as you a) use the correct package name (for that particular package it&#39;s &#34;bitstream&#34;, as evident from the header here and b)</p> </blockquote> <p>o_O</p> <p>Thanks! I hadn&#39;t even realized I had this problem yet. What I was troubleshooting was <code>go-bitstream.NewReader()</code> because in the examples I&#39;ve seen so far you use the last bit of the import path to reference package functions. I thought I was hung up on io.Reader, but you just made me notice I wasn&#39;t even using the right package name: <code>bitstream.NewReader()</code></p> <p>And when I used that and hit save, this happened:</p> <pre><code>package main import ( &#34;fmt&#34; &#34;log&#34; &#34;os&#34; &#34;strings&#34; bitstream &#34;github.com/dgryski/go-bitstream&#34; ) </code></pre> <p>Thanks again!</p></pre>TheMerovius: <pre><p>That&#39;s one of the reasons why you shouldn&#39;t do that (i.e. have a different package-name than the last component of the path) :) Anyway, I hope you learn to love goimports as the rest of the community does :) And welcome to the community, while we&#39;re at it :)</p></pre>RandNho: <pre><p>I think best option on that stage would be to go and switch default formatter from goimports to gofmt in settings.</p></pre>szabba: <pre><p>That&#39;s one way to go about it, certainly. With third party packages goimports can not only take a long time, but also import the wrong one (when multiple have the same package name).</p></pre>TheMerovius: <pre><blockquote> <p>when multiple have the same package name</p> </blockquote> <p>…and the same functions, and types. That really shouldn&#39;t happen very often. In practice, I manually have to edit imports approximately only when importing them under different names (e.g. for generated proto packages, which have annoying names).</p></pre>szabba: <pre><p>The go-gl binding suffer from this too!</p></pre>KSubedi: <pre><p>do a <code>go get</code> on the repository url of <code>go-bitstream</code> and it should auto detect things from there</p></pre>midnightFreddie: <pre><p>Ok, thanks.</p> <p>Personally I&#39;d much rather get an error than have it delete code, but that&#39;s between me and my VSCode extensions I suppose.</p></pre>aboukirev: <pre><p>In default settings the &#34;go.formatTool&#34; setting is &#34;goreturns&#34;, which causes automatic handling of imports depending on usage. If you override that with &#34;gofmt&#34; in your user settings, then it won&#39;t delete the import line but instead will highlight it with red squiggle as an error (unused imported package). I&#39; personally, much prefer the latter behavior.</p></pre>SeerUD: <pre><p>If you can get <code>goimports</code> set up then that should automatically add and remove them.</p></pre>

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

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