mvdan / gofumpt

A stricter gofmt
https://pkg.go.dev/mvdan.cc/gofumpt
BSD 3-Clause "New" or "Revised" License
3.15k stars 110 forks source link

Members of struct after Pass-through type inside struct's formatting is ignored #282

Closed apprehensions closed 10 months ago

apprehensions commented 10 months ago

This Go snippet (couldn't think of a better name):

type Vocalization string

type Feline struct {
    MeowMeowMrrp string
    Miaow Vocalization
    Vocalization
}

works fine, as gofumpt formats it as so:

type Feline struct {
    MeowMeowMrrp string
    Miaow        Vocalization
    Vocalization
}

however if the struct's order is like so:

type Feline struct {
    MeowMeowMrrp string
    Vocalization
    Miaow Vocalization
}

it will remain as is. Is this intentional? or because of the passthrough struct not having a name inside the struct, gofumpt cannot calculate the formatting width?

apprehensions commented 10 months ago

Also, shouldn't the struct members be organized? organizing them may save memory.

mvdan commented 10 months ago

it will remain as is. Is this intentional?

This is a formatting choice by gofmt. gofumpt will never fight gofmt on its formatting choices, per the README. I believe gofmt "breaks" vertical alignment in this case on purpose.

Also, shouldn't the struct members be organized? organizing them may save memory.

gofumpt is a formatting tool, that has nothing to do with formatting. Plus, reordering struct fields can be a breaking change and should never be done automatically.