Linter for field order in struct initialization #4919
-
Hello, do you know of a linter that will error if a struct is initialized with a different field order than the struct definition? For example: // Given this struct definition:
type Foobar struct {
Foo int
Bar int
}
// This should succeed:
var a = Foobar{
Foo: 3,
Bar: 14,
}
// But this should fail:
var b = Foobar{
Bar: 14,
Foo: 3,
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hello, I don't think a linter exists for that as it's a pure "style" linter. Can you explain the need behind this? Why the order is important for you? |
Beta Was this translation helpful? Give feedback.
-
Hi @begelundmuller, I created a linter that does what you want here: https://github.com/manuelarte/structfieldinitorder In the README.md you should find the instructions on how to run it. Let me know how it goes if you use it. After some talks with the maintainers of golangci-lint, I don't think this linter will be included in it. |
Beta Was this translation helpful? Give feedback.
Hi @begelundmuller,
I created a linter that does what you want here: https://github.com/manuelarte/structfieldinitorder
In the README.md you should find the instructions on how to run it. Let me know how it goes if you use it.
After some talks with the maintainers of golangci-lint, I don't think this linter will be included in it.