Skip to content

proposal: cmd/gofmt: A change of spacing within for...range <int> loops #65643

@adamroyjones

Description

@adamroyjones

Proposal Details

I know this is dangerously close to bike-shedding but I thought it worth the suggestion, even if it ends up being closed.

Here's a quite tortured example that's derived from something slightly less tortured.

package main

func main() {
	xs := []int{0, 1, 2}
	n := 1
	ys := make([]int, 0, len(xs)-(n-1))
	for range len(xs)-(n-1) {
		ys = append(ys, len(xs)-(n-1))
	}
}

When passed through gofmt, this becomes

package main

func main() {
	xs := []int{0, 1, 2}
	n := 1
	ys := make([]int, 0, len(xs)-(n-1))
	for range len(xs) - (n - 1) {
		ys = append(ys, len(xs)-(n-1))
	}
}

which, to my eye, seems worse. The terms after the range keyword form a tight conceptual cluster, but the text is spread.

I didn't see any discussion on #61405 about associated gofmt rules, so this may be an unintended outcome.

I propose that the spacing be as with function calls. I'm happy to have a stab at an implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions