Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for VS Code, Golang formatting stubborn situation with switch/case

$
0
0

It is, indeed, the Go style to not indent inside a switch for some reason, possibly a hold over from C styles. For example, this is the style used in Effective Go. In addition, the Go community is generally against changing the style and their tools generally are not configurable. VSCode's own language server and all your mentioned Go tools will follow this style.

However, goformat will let you adjust the style. For example, indent=2 switch enter=1 will give you a more familiar style with 2 character whitespace indentation and switch blocks will be indented.

goformat.exe -style "indent=2 switch enter=1" .\tmp\test.gopackage mainimport "fmt"func main() {  myString := "foo"  switch {    case myString == "":      fmt.Println("Empty string")    case myString == "foo":      fmt.Println("foo")    case myString == "bar":      fmt.Println("We got bar now!")    default:      fmt.Println("Another default")  }}

VSCode can be configured to use goformat. In the Go extension search for "Format Flags" and "Format Tool". You can put your style choices into a file and commit it with your project.


Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>