How to set vscode format golang code on save?

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

How to set vscode format golang code on save?

问题

我正在使用vscodeGo扩展来编辑golang源代码。每次我想要格式化代码时,我都需要在Linux上按下Ctrl-Shift-i,在Windows上按下Shift-Alt-F,或者按下Ctrl-Shift-p然后输入format。是否可以设置在保存时自动进行格式化,也就是说,当我按下ctrl-s时,它会自动使用go.fmt(或类似的工具)来格式化代码?

英文:

I'm using vscode with Go extensions to edit golang source code. Each time I want to format code, I have to press Ctrl-Shift-i on linux, Shift-Alt-F on Windows, or Ctrl-Shift-p and type format. Is it possible to set format on save, that is, when I press ctrl-s, it format the code automatically using go.fmt (or something alike)?

答案1

得分: 28

对我来说,没有一个答案有效。我的Go版本是1.17.1,VSCode版本是1.60.1,我正在使用Linux Pop!_os。

在网上搜索后,在官方VSCode Go文档中找到了以下内容:https://code.visualstudio.com/docs/languages/go#_formatting

我的settings.json文件如下所示:

"": {
    "editor.insertSpaces": true,
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "golang.go"
},

注意:您需要在VS Code中安装所需的Go语言扩展。打开一个*.go文件后,检查左下角的状态栏,您应该看到Go版本。如果看到一个感叹号图标,请单击它并安装建议的扩展。

英文:

For me, none of the answers worked. My Go version is 1.17.1, VSCode version is 1.60.1 and I'm using Linux Pop!_os.

After some digging online found this in the official VSCode documentation for Go. https://code.visualstudio.com/docs/languages/go#_formatting

My settings.json looks like this

"": {
	"editor.insertSpaces": true,
	"editor.formatOnSave": true,
	"editor.defaultFormatter": "golang.go"
},

Note: You need to install the required extensions for the go lang in VS code. Check the bottom left bar after opening a *.go file and you should see the go version. If you see an exclamation icon then click on it and install the suggested extensions.

答案2

得分: 26

根据我的 Visual Code 版本,我无法使用 config go.formatOnSave: false

然后我可以通过以下设置关闭它们:

  • 构建(使用 go.buildOnSave 设置关闭)
  • Lint(使用 go.lintOnSave 设置关闭)
  • Vet(使用 go.vetOnSave 设置关闭)
  • 格式化(通过在设置中添加以下内容关闭):
"go": {
    "editor.formatOnSave": false
}
英文:

From my visual code version, i cannot use config go.formatOnSave": false.

Then I can turn them off in settings as below:

  • Build (Turn off using go.buildOnSave setting)
  • Lint (Turn off using go.lintOnSave setting)
  • Vet (Turn off using go.vetOnSave setting)
  • Format (Turn off by adding the below in your settings):
    > "": {
    > "editor.formatOnSave": false
    > }

答案3

得分: 24

你应该安装这个插件:https://github.com/golang/vscode-go。其中一个选项是在保存时设置“自动格式化”:go.formatOnSave": false。它使用Golang工具进行格式化。

英文:

You should install this plugin: https://github.com/golang/vscode-go. One of the options is to set "auto format" on save: go.formatOnSave": false. It uses the Golang tooling for formatting.

答案4

得分: 7

对我来说,以下设置起作用了。我禁用了烦人的导入重构。

"": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": false
    },
}
英文:

For me the followed settings worked. I disabled the annoying import refactoring.

"": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": false
    },
}

huangapple
  • 本文由 发表于 2016年2月23日 15:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/35571033.html
匿名

发表评论

匿名网友

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

确定