dongyi4170 2014-11-09 02:10
浏览 133
已采纳

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"
)
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料