Deprecated: Use https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment instead.
Install:
go get github.com/mdempsky/maligned
Usage:
maligned cmd/compile/internal/gc cmd/link/internal/ld
Tool to detect Go structs that would take less memory if their fields were sorted.
License: BSD 3-Clause "New" or "Revised" License
Deprecated: Use https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment instead.
Install:
go get github.com/mdempsky/maligned
Usage:
maligned cmd/compile/internal/gc cmd/link/internal/ld
maligned.go containing the analysis pass appears to be missing from the passes/maligned directory.
Hi,
I am wondering if I can explicitly skip running maligned
check on any given struct rather than globally checking either all or none?
Regards
This is how I wish to return JSON response
{
"id": 1,
"name": "hello",
"is_active": true,
"created_at": "2019-01-01"
}
struct{
ID uint16
Name string
IsActive bool
CreatedAt string
}
However, it is not possible because struct is not maligned and I have to malign it and the response looks odd.
struct{
Name string
CreatedAt string
ID uint16
IsActive bool
}
{
"name": "hello",
"created_at": "2019-01-01",
"id": 1,
"is_active": true
}
Some annotation solution like this, maybe!
// @skip: maligned
struct{
ID uint16
Name string
IsActive bool
CreatedAt string
}
Given the code:
model.go
package addrs
// ASDExchanger bla bla
type ASDExchanger struct {
ASDBtcExchangeRate string `mapstructure:"asd_btc_exchange_rate"`
ASDBtcExchangeRateUSD string `mapstructure:"asd_btc_exchange_rate_usd"`
ASDBtcExchangeLabel string `mapstructure:"asd_btc_exchange_label"`
ASDBtcExchangeEnabled bool `mapstructure:"asd_btc_exchange_enabled"`
ASDEthExchangeRate string `mapstructure:"asd_eth_exchange_rate"`
ASDEthExchangeRateUSD string `mapstructure:"asd_eth_exchange_rate_usd"`
ASDEthExchangeLabel string `mapstructure:"asd_eth_exchange_label"`
ASDEthExchangeEnabled bool `mapstructure:"asd_eth_exchange_enabled"`
ASDSkyExchangeRate string `mapstructure:"asd_sky_exchange_rate"`
ASDSkyExchangeRateUSD string `mapstructure:"asd_sky_exchange_rate_usd"`
ASDSkyExchangeLabel string `mapstructure:"asd_sky_exchange_label"`
ASDSkyExchangeEnabled bool `mapstructure:"asd_sky_exchange_enabled"`
ASDWavesExchangeRate string `mapstructure:"asd_waves_exchange_rate"`
ASDWavesExchangeRateUSD string `mapstructure:"asd_waves_exchange_rate_usd"`
ASDWavesExchangeLabel string `mapstructure:"asd_waves_exchange_label"`
ASDWavesExchangeEnabled bool `mapstructure:"asd_waves_exchange_enabled"`
ASDWavesASDExchangeRate string `mapstructure:"asd_waves_asd_exchange_rate"`
ASDWavesASDExchangeRateUSD string `mapstructure:"asd_waves_asd_exchange_rate_usd"`
ASDWavesASDExchangeLabel string `mapstructure:"asd_waves_asd_exchange_label"`
ASDWavesASDExchangeEnabled bool `mapstructure:"asd_waves_asd_exchange_enabled"`
}
When I run gometalinter and maligned:
gometalinter --deadline=3m -j 2 --disable-all --tests --vendor \
-E maligned \
./...
Then throws me err:
model.go:4:19:warning: struct of size 280 could be 248 (maligned)
When I remove the bool and or replace with string, it works with no errs.
Any ideas ?
๐๐ฝ hi
awesome tool! I see it is deprecated, so I think it's best to archive the repo, which will keep it public, but warn the users that it is no longer maintained ๐
hi, i know that fieldalignment now preferred way to check/fix field alignment in golang structs.
how to deal with it if i want to have automatic align fields via own golang program? (for example i'm generate code from protobuf)
since gotool doesn't support go mod,and it recommend use go/packages
see kisielk/gotool#20
As pointed out on Twitter. This would add support for modules, and also modernize the tool quite a bit as go/loader hasn't been supported in years.
When done, please update golang/go#24661 with a comment so that the "maligned" checklist item can be ticked off.
I got this compilation error while executing go get github.com/mdempsky/maligned
with Go1.5.1:
./maligned.go:50: impossible type assertion:
*"golang.org/x/tools/go/types".Struct does not implement "go/types".Type (wrong type for Underlying method)
have Underlying() "golang.org/x/tools/go/types".Type
want Underlying() "go/types".Type
github.com/golang/tools
introduced an API change for go/types around commit: golang/tools@2477c0d.
Since c37866e, maligned complains on duplicate methods in different custom build tags.
main.go
package main
func main() {
debug("x")
}
debug.go
// +build debug
package main
func debug(s string) { println(s) }
release.go
// +build !debug
package main
func debug(string) {}
% maligned *.go
/tmp/release.go:5:6: debug redeclared in this block
/tmp/debug.go:5:6: other declaration of debug
/tmp/release.go:5:6: debug redeclared in this block
/tmp/debug.go:5:6: other declaration of debug
maligned: 4 errors during loading
Before c37866e, maligned worked well in this situation.
This is a great project. Thank you for sharing it with the community.
I discovered the project when gometalinter ran against some of my code and found an inefficient alignment in one of my code's structs. But when I looked up the project, I found nothing in the README or the GoDoc that explained why memory alignment is a good thing or how I might achieve it.
I read Padding is hard which helped, but wonder if we might add something in the README or even the source code to help others who might have the same problem.
/cc @markmandel
$ golint
[works on .]
$ go vet
[works on .]
$ maligned
2016/07/07 17:42:30 no initial packages were loaded
$ maligned .
[works on .]
When scanning objects during GC, the standard compiler/runtime only scan as far as the last pointer slot in an object. If fields are sorted so that all of the pointer-y fields come first, then the GC may be able to short-circuit its scan work slightly.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.