Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/gofmt: indenting of multi-line case list is confusing #48065

Closed
ghost opened this issue Aug 30, 2021 · 2 comments
Closed

cmd/gofmt: indenting of multi-line case list is confusing #48065

ghost opened this issue Aug 30, 2021 · 2 comments

Comments

@ghost
Copy link

ghost commented Aug 30, 2021

What version of Go are you using (go version)?

$ go version
go version go1.17 linux/amd64

What did you do?

I pass this file to gofmt:

package main

import "fmt"
import "math/rand"

func main() {
r := rand.Intn(5)
switch r {
case 0:
fmt.Println("It's zero!")
case 1, 
2:
fmt.Println("It's one or two!")
default:
fmt.Println("It's something else")
}
}

What did you see?

package main

import "fmt"
import "math/rand"

func main() {
	r := rand.Intn(5)
	switch r {
	case 0:
		fmt.Println("It's zero!")
	case 1,
		2:
		fmt.Println("It's one or two!")
	default:
		fmt.Println("It's something else")
	}
}

Notice how the case list of case 1,2 blends into the case's code.

What did you expect?

I can think of two options:

  • Add two indentation levels when starting the second line of the multi-line case list, and remove one indentation level when entering the case
package main

import "fmt"
import "math/rand"

func main() {
	r := rand.Intn(5)
	switch r {
	case 0:
		fmt.Println("It's zero!")
	case 1,
			2:
		fmt.Println("It's one or two!")
	default:
		fmt.Println("It's something else")
	}
}
  • Align with the first case of the case list
package main

import "fmt"
import "math/rand"

func main() {
    r := rand.Intn(5)
    switch r {
    case 0:
        fmt.Println("It's zero!")
    case 1,
         2:
        fmt.Println("It's one or two!")
    default:
        fmt.Println("It's something else")
    }
}
@ghost
Copy link
Author

ghost commented Aug 30, 2021

I don't know the logic of gofmt. This might be the same issue as #48064. Sorry to double submit if this is the case.

@cherrymui
Copy link
Member

No worries. It indeed looks like a dup. Closing.

@golang golang locked and limited conversation to collaborators Sep 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants