gofmt consistency improvements

656 views
Skip to first unread message

Matt Ho

unread,
Jan 20, 2018, 11:54:14 PM1/20/18
to golang-dev
We love gofmt as a tool, especially the way it aligns declarations and assignments on things like structs and maps.  I think this adds significantly to readability.  

The proposal is to extend the struct and map alignment to include var and := declarations to make formatting more consistent.

For example:

abc := 123
d := 456

Becomes

abc := 123
d   := 456

To keep things simple, this would only be for := that appear in sequence or = that appear in sequence.  

So this:

abc := 123
d = 456

Is left alone as := and = don't interact with each other.  I'd also suggest that var = and plain = don't interact either.  So:

abc  = 123
var def = 456

Would also be left alone as = and var = wouldn't interact.

Here are some other examples:

a.b.c = 1
a.d = 2
a.e.f.g = "hello"
h, i = 1, 2

Becomes:

a.b.c   = 1
a.d     = 2
a.e.f.g = "hello"
h, i    = 1, 2

Vars would be treated much like structs with the field, type, and metadata being aligned.

var a string = "string"
var a,b int = 1,2

Becomes

var a    string = "string"
var a, b int    = 1,2


This would use the same rules as structs and maps for determining when to continue alignment and when alignment should be broken up because its too large.

Thoughts?



jerome....@gmail.com

unread,
Jan 21, 2018, 2:25:07 AM1/21/18
to golang-dev
Hello,
For short assignment, you can use:
var (
ab = 1
c = 2
)

Jan Mercl

unread,
Jan 21, 2018, 5:37:44 AM1/21/18
to Matt Ho, golang-dev
On Sun, Jan 21, 2018 at 5:54 AM Matt Ho <mat...@gmail.com> wrote:
 
For example:

abc := 123
d := 456

Becomes

abc := 123
d   := 456

No, no, no, please no.



--

-j

Steven Hartland

unread,
Jan 21, 2018, 4:46:13 PM1/21/18
to Jan Mercl, Matt Ho, golang-dev
Please no, alignment formatting is horrible as it makes simple changes that add new lines introduce many unneeded changes making it much harder to see the real changes in diffs.

--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Ho

unread,
Jan 21, 2018, 8:32:46 PM1/21/18
to golang-dev
This is really the same formatting that gofmt already does, just applied to a wider scope.

Jan Mercl

unread,
Jan 22, 2018, 5:02:50 AM1/22/18
to Matt Ho, golang-dev
On Mon, Jan 22, 2018 at 2:32 AM Matt Ho <mat...@gmail.com> wrote:

> This is really the same formatting that gofmt already does, just applied to a wider scope.

No. Gofmt does this only for declarations wrapped in a block inside parenthesis. The proposal is to do it for statements within normal code. For a block of declarations it's okay, but tabular layout of statements makes no sense to me.



--

-j

John Souvestre

unread,
Jan 22, 2018, 5:56:11 AM1/22/18
to golan...@googlegroups.com

Some compare programs have a switch which ignores differences in spacing.

 

John

    John Souvestre - New Orleans LA

Seth W. Klein

unread,
Jan 27, 2018, 3:17:03 PM1/27/18
to golang-dev
If you want alignment of a block of variables, you can use a var block:

```
package main

import (
"fmt"
)

func main() {
var (
one = 1
three = 3
)
fmt.Println(i + j)
}

```

https://play.golang.org/p/HZTO3Elkx04

wells.n...@gmail.com

unread,
Jan 31, 2018, 6:08:58 PM1/31/18
to golang-dev
I really like this idea - it makes it much easier to visually scan down a list of assignments and compare the values from line to line.

another change I'd like to see would be to indent function arguments not on the same line as the function declaration by an extra tab so that they don't line up with the first line of the function body 

Jim Robinson

unread,
Feb 1, 2018, 1:21:56 PM2/1/18
to golang-dev
I wouldn't like that.  I use an editor that uses a variable width font and attempts at 'alignment' detracts from readability.

Nick Wells

unread,
Feb 1, 2018, 1:55:32 PM2/1/18
to Jim Robinson, golang-dev
Hi Jim, surely with a variable width font you don't care about alignment. Gofmt already aligns structs so it's just a small extension of that

Nick
On Thu, 1 Feb 2018 at 18:22, Jim Robinson <jim.ro...@gmail.com> wrote:
I wouldn't like that.  I use an editor that uses a variable width font and attempts at 'alignment' detracts from readability.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-dev/HmVtKoXGYZU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-dev+...@googlegroups.com.

James A. Robinson

unread,
Feb 1, 2018, 1:56:51 PM2/1/18
to Nick Wells, golang-dev
Well, I'm just registering a comment that for me alignment would 'make it worse' rather than 'make it better.' :)

Ralph Corderoy

unread,
Feb 1, 2018, 2:18:17 PM2/1/18
to golang-dev
Hi,

These `format wars' are not worthy of golang-dev IMHO. Could they be
thrashed out, and very probably shot down, on nuts or a Github Go2
issue? I'm sure alignment of the type suggested was well considered by
Go's designers, and quite possibly argued about ad nauseam by the coffee
machine, for `go fmt's benefit so we wouldn't have to suffer. :-)

--
Cheers, Ralph.
https://plus.google.com/+RalphCorderoy

Robert Griesemer

unread,
Feb 1, 2018, 2:25:10 PM2/1/18
to Ralph Corderoy, golang-dev
Thanks for your suggestion, Ralph. Agreed.

That said, I do keep an eye on gofmt issues filed via the issue tracker (that doesn't mean they will all get accepted). If we ever get to an overhaul/modernization of gofmt, we will certainly review those issues. And every once in a while I do fix minor issues that come up. So please keep high-quality feedback coming. Thanks.

- gri


--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.

yogesh...@tokopedia.com

unread,
Feb 27, 2018, 2:27:00 PM2/27/18
to golang-dev
I would also want to see the formatting as Jan Mercl suggested. I believe it makes the code more readable and makes sense to have such kind of formatting in normal code. Though "gofmt" does it for a block of variables or struct declarations.
Reply all
Reply to author
Forward
0 new messages