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
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
Since c37866e2d38b8e33600b215e4095af870becd4d8, 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 c37866e2d38b8e33600b215e4095af870becd4d8, maligned worked well in this situation.
This seems much simpler and more reliable than hard-coding the sizes. Additionally, it seems strange that types.Int and types.Uint weren't originally included in the basicSizes map. Maybe they were defined as aliases of size-specific types when maligned was written but have since been redefined as unique types?
For complex structs, it's almost impossible to find the correct combination without knowing the fine details of the alignment rules.
If it was easy, this whole tool would not be needed at all.
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.
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: https://github.com/golang/tools/commit/2477c0d5780ddec6439a9322a6862dfe20cc8b81.
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 https://github.com/golang/go/issues/24661 with a comment so that the "maligned" checklist item can be ticked off.
Support import path patterns, including within relative import paths. When given 0 arguments, use the package in current working directory. If the import path patterns match no packages, print a warning and exit with 0 exit code.
Rely on go/loader to resolve relative import paths relative to current working directory.
Fixes #4.
Similar to mdempsky/unconvert#9.
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
}
👋🏽 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)
Add -v
to display fields where padding is added.
Example:
...\encoder_options.go:14:21: struct of size 64 could be 56
Field sizes in order:
* concurrent int: 8:8
* crc bool: 1:1
!!! single *bool: 8:15
* pad int: 8:8
* blockSize int: 8:8
* windowSize int: 8:8
* level github.com/klauspost/compress/zstd.EncoderLevel: 8:8
* fullZero bool: 1:1
* noEntropy bool: 1:1
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
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 ?
executor fast exec task with go and less mem ops Why we need executor? Go with g
golauncher A go application launcher A simple, highly extensible, customizable application launcher and window switcher written in less than 300 lines
wordier Solver for wordle hard mode - achieves 5 attempts or less 100% of the time Example - Spoiler ➜ wordier git:(master) ✗ go run main.go scamp ➜
customer.io full stack engineer take home project
typex Examine Go types and their transitive dependencies. Export results as TypeScript value objects (or types) declaration. Installation go get -u gi
RTS: Request to Struct Generate Go structs definitions from JSON server responses. RTS defines type names using the specified lines in the route file
defaults Initialize structs with default values Supports almost all kind of types Scalar types int/8/16/32/64, uint/8/16/32/64, float32/64 uintptr, bo
Shoutrrr Notification library for gophers and their furry friends. Heavily inspired by caronc/apprise. Quick Start As a package Using shoutrrr is easy
Gorsair Gorsair is a penetration testing tool for discovering and remotely accessing Docker APIs from vulnerable Docker containers. Once it has access
This project is an implementation of Fermat's factorization method in which multiples of prime numbers are factored into their constituent primes. It is a vanity attempt to break RSA Encryption which relies on prime multiples for encryption.
Peekprof Get the CPU and Memory usage of a single process, monitor it live, and extract it in CSV and HTML. Get the best out of your optimizations. Us
Package stats Package stats allows for gathering of statistics regarding your Go application and system it is running on and sent them via UDP to a se
memory-enhancer Helps exercise your memory by giving you random tokens and poems to memorize. Using Every day when you first open your terminal you wi
Implement a toy in-memory store information service for a delivery company
This is an in-memory key-value store HTTP API service, with the following endpoints: /get/{key} : GET method. Returns the value of a previously set ke
Easy to Use In-Memory Key-Value Store A project that provides an in-memory key-value store as a REST API. Also, it's containerized and can be used as
In memory of my dog Safira. The tool safira replaces pattern_offset.rb and patte
proc-peepin Capture process cpu and memory and send it off to influx Running loc
The forgotten go tool that executes and caches binaries included in go.mod files. This makes it easy to version cli tools in your projects such as gol