dongyi4170
2014-11-09 02:10
浏览 114

vim-go写入磁盘时删除未使用的代码

I have imported several packages into a file in a Go package (package commands) I am writing (following along with a golang tutorial) using Vim with the Vim-Go plugin. Several of these packages are not yet used in the package. When I save the file :w, Vim seems to be deleting the unused packages, which is really annoying because those unused packages are going to be used. I just haven't added the necessary code for them. Is there a way to turn off this functionality in Vim-Go or do I have to delete the whole plugin to get rid of this annoying behavior?

Before Write

package commands
import (
    "fmt"
    "os"
    "time"

    rss "github.com/jteeuwen/go-pkg-rss"
    "github.com/spf13/cobra"
    "github.com/spf13/viper"
)

After Save

import (
    "github.com/spf13/cobra"
    "github.com/spf13/viper"
)

图片转代码服务由CSDN问答提供 功能建议

我已将多个软件包导入Go软件包的文件中( package命令) 正在使用带有Vim-Go插件的Vim编写(跟随golang教程)。 这些软件包中有几个尚未在软件包中使用。 当我保存文件:w 时,Vim似乎正在删除未使用的软件包,这确实很烦人,因为这些未使用的软件包将被使用。 我只是没有为他们添加必要的代码。 有没有办法在Vim-Go中关闭此功能,还是我必须删除整个插件才能摆脱这种烦人的行为?

写之前 \ n

 包命令
import(
“ fmt” 
“ os” 
“ time” 
 
 rss“ github.com/jteeuwen/go-pkg-rss"
” github。  com / spf13 / cobra“ 
” github.com/spf13/viper"
)
  
 
 

保存后

   import(
“ github.com/spf13/cobra"
” github.com/spf13/viper"
)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanji1026 2014-11-09 02:15
    已采纳

    This is because you're using goimports as the tool to use when formatting your go code (I think vim-go does this by default - and it formats code on save by default). goimports removes unused imports for you .. thats why its so great. You will learn to love it at some point :)

    For now, what you want to use is gofmt to format your code, which doesn't touch imports - it only formats the code. You can put this in your .vimrc:

    let g:go_fmt_command = "gofmt"
    

    If you decide you want to manually run goimports on your file after you've done the above .. you can run :GoImports.

    You can also choose another option if you prefer: you can turn off formatting on save by putting this into your .vimrc:

    let g:go_fmt_autosave = 0
    

    Then, you can still use goimports when formatting your code .. but it won't do it automatically on save.

    点赞 打赏 评论

相关推荐 更多相似问题