- 我正在使用 VSCode
1.87.2
,操作系统是 Ubunu 23.10 - 使用 Go 扩展
v0.41.2
- 我出于测试目的禁用了所有其他扩展
- 使用
go1.21.0 linux/amd64
我在 Golang 文件中编写了这段代码*.go
:
switch {
case myString == "":
fmt.Println("Empty string")
case myString == "foo":
doFoo()
case myString == "bar":
fmt.Println("We got bar now!")
default:
fmt.Println("Another default")
}
VS code 将其格式化为:
switch {
case myString == "":
fmt.Println("Empty string")
case myString == "foo":
doFoo()
case myString == "bar":
fmt.Println("We got bar now!")
default:
fmt.Println("Another default")
}
问题,有没有一种方法可以合理地配置 VSCode/Go 扩展/其他(即不必编写自己的 VS Code 扩展或绕过不寻常的奇怪设置),以便保留片段 1 的格式?
我尝试过的:
- 尝试过格式化程序:
gofumpt
,gofmt
,goimports
- 尝试禁用所有扩展(除了 go)。
不知道出了什么问题。
事实上,
switch
由于某种原因,Go 风格不在 a 内缩进,这可能是 C 风格的延续。例如,这是Effective Go中使用的样式。此外,Go 社区普遍反对改变风格,而且他们的工具通常是不可配置的。VSCode 自己的语言服务器和您提到的所有 Go 工具都将遵循这种风格。不过,
goformat
会让你调整风格。例如,indent=2 switch enter=1
将为您提供更熟悉的样式,其中包含 2 个字符空白缩进,并且开关块将缩进。VSCode 可以配置为使用
goformat
. 在 Go 扩展中搜索“格式标志”和“格式工具”。您可以将您的样式选择放入文件中并将其与您的项目一起提交。