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,
} |
Answered by
manuelarte
Jun 14, 2025
Replies: 2 comments 3 replies
|
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? |
1 reply
|
Hi @begelundmuller, I created a linter that does what you want here: https://github.com/manuelarte/structinit 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. |
2 replies
Answer selected by
ldez
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @begelundmuller,
I created a linter that does what you want here: https://github.com/manuelarte/structinit
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.