在Sublime Text中保存时运行goimports吗?

huangapple go评论97阅读模式
英文:

Run goimports on save in Sublime Text?

问题

在Sublime Text 3中,安装了插件GoSublimeGoImports

我在每次保存时自动运行goimports时遇到了问题。

这是我尝试过的方法:

我的GoSublime设置如下:

{
    "env": {"GOPATH": "/home/nicolas/.go", "PATH": "$GOPATH/bin:$PATH" },
    //"fmt_cmd": ["goimports"]
}

使用这些设置保存时,代码会被格式化,但是导入语句没有被添加。到目前为止没有什么意外。

如果我按下ctrl+shift+P然后选择GoImports,导入语句会按预期添加。

现在我的问题是,当我取消注释上述行以使用以下设置时:

{
    "env": {"GOPATH": "/home/nicolas/.go", "PATH": "$GOPATH/bin:$PATH" },
    "fmt_cmd": ["goimports"]
}

不仅没有添加导入语句,而且其他格式化也不再执行。

你有任何想法我哪里出错了吗?

我正在使用Linux(Ubuntu)。

英文:

In Sublime Text 3, with plugins GoSublime and GoImports installed.

I'm having trouble having goimports run on my file automatically everytime I save.

Here's what I tried :

My GoSublime settings are set to :

{
    "env": {"GOPATH": "/home/nicolas/.go", "PATH": "$GOPATH/bin:$PATH" },
    //"fmt_cmd": ["goimports"]
}

When I save with these settings, the code gets formatted but imports are not added. Nothing surprising so far.

If I hit ctrl+shift+P then GoImports, imports get added as expected.

Now my problem is, when I uncomment that previous line to have these settings :

{
    "env": {"GOPATH": "/home/nicolas/.go", "PATH": "$GOPATH/bin:$PATH" },
    "fmt_cmd": ["goimports"]
}

Not only does it not add the imports, but no other formatting gets done anymore.

Any idea where I went wrong ?

I'm using Linux (Ubuntu).

答案1

得分: 10

确保$GOPATH/bin在你的$PATH中(Windows:%GOPATH%\bin放在%PATH%中)。
运行go get -u golang.org/x/tools/cmd/goimports(你可能需要安装mercurial)。
安装Sublime Text和GoSublime(或者确保你已经安装了最新的更新)。
打开GoSublime用户config/preference文件(Mac:⌘. ⌘5 Windows:Ctrl+. Ctrl+5)。确保在整个快捷键序列中一直按住命令按钮。
使其看起来像这样:
{"fmt_cmd": ["goimports"]}

来源

英文:
  1. Make sure $GOPATH/bin is in your $PATH (Windows: %GOPATH%\bin goes
    in your %PATH%).
  2. Run go get -u golang.org/x/tools/cmd/goimports (you may have to
    install mercurial).
  3. Install Sublime Text and GoSublime (or make sure you’ve got the
    latest update if it’s already installed).
  4. Open the GoSublime user config/preference file (Mac: ⌘. ⌘5 Windows:
    Ctrl+. Ctrl+5). Make sure you keep the command button down for the
    whole shortcut sequence.
  5. Make it look like this:
    {"fmt_cmd": ["goimports"]}

Source

答案2

得分: 3

所以,我做错了什么:

我在Sublime中使用Package control > Install package > GoImports安装了GoImports。这将它安装在Sublime Packages文件夹中,而该文件夹不在我的$PATH中。

我应该做的是:

使用命令go get -u golang.org/x/tools/cmd/goimports进行安装。这将在您的$GOPATH中安装它($GOPATH/bin应该在您的$PATH中)。

英文:

So, where I went wrong :

I installed GoImports from inside Sublime using Package control > Install package > GoImports. That installed it in the Sublime Packages folder, which was not on my $PATH

What I should have done instead :

Install it using the command go get -u golang.org/x/tools/cmd/goimports. This will install it in your $GOPATH (and $GOPATH/bin should be on your $PATH).

答案3

得分: 0

ctrl+. , ctrl+x 打开 margo.go 文件。
然后取消注释 golang.GoFmtgolang.GoImports 中的一个。重新启动 Sublime Text 并再次尝试。

// golang.GoFmt,
// 或者
// golang.GoImports,

我使用的是 ubuntu + sublime text 3207 + Go-1.10 + Gosublime + Goimports-release-1.10。在取消注释 golang.GoImports 后它可以正常工作。

英文:

ctrl+. , ctrl+x open margo.go file.
Then uncomment one of golang.GoFmt & golang.GoImports. restart sublime text and try again.

// golang.GoFmt,
// or
// golang.GoImports,

I using ubuntu + sublime text 3207 + Go-1.10 + Gosublime + Goimports-release-1.10. And it works for after I uncomment golang.GoImports.

huangapple
  • 本文由 发表于 2016年4月22日 05:49:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/36781093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定