Gocondense: a gofmt that condenses verbose Go code

Hi all,

I’ve been working on a new code formatter for condensing Go code that feels far more verbose than it needs to be.

It takes multi-line constructs and pulls them onto a single line, while respecting preferred line length and preserving inline comments. Like prettier, it only condenses struct and map literals if the first element is on the same line as the opening brace. It also does a bunch of cleanup like removing unnecessary parentheses, grouping adjacent params of the same type and unwrapping single-item declaration groups. Since it’s a superset of gofmt it’s pretty much a drop-in replacement with added functionality.

Here’s a small example of the kind of changes it makes:

// before
func Add[
	T ~int,
](
	a T,
	b T,
) (
	result T,
) {

	return a + b

}

// after
func Add[T ~int](a, b T) (result T) {
	return a + b
}

Repo (with more examples, config options, and editor integration) is here:

If you try it out, I’d love to hear your feedback - good or bad!

2 Likes

OK this is cool and I’m not knocking it. But who has EVER made a function like your example? :rofl:

When you say it is “a superset of gofmt”, what do you mean? I didn’t dig into the source much.

It’s exaggerated for dramatic effect :winking_face_with_tongue:
The initial version was actually born from frustration of people pushing overly verbose AI slop code to a project I was working on a while back.

By superset I mean that running gofmt on code formatted with gocondense will never result in further changes.