-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
What version of Go are you using (go version
)?
$ go version go version devel go1.19-9a53b472b5 Thu Mar 31 21:39:51 2022 +0000 darwin/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env
)?
Not applicable
What did you do?
Per https://twitter.com/odeke_et/status/1520929877104332801, over the weekend while working on some code for an embedded system, I examined the binary size and noticed the use of fmt.Errorf without format specifiers or just constant strings was permitted and could instead use errors.New of which the results of swapping the change caused a 600KiB reduction in binary size on Linux AMD64, and 700KiB reduction in binary size on Darwin AMD64!
Just compiled this simple program https://go.dev/play/p/Ec6Wy1sUpF_e or inlined below
package main
import "fmt"
func main() {
panic(fmt.Errorf("This is a call!"))
}
On Linux it results in a 1.8MiB binary on both Linux AMD64 and Darwin AMD64
go build -o fmt-binary main.go && ls -lh
-rwxrwxr-x 1 emmanuel emmanuel 1.8M May 1 17:41 fmt-binary
On changing it to use errors.New
package main
import "errors"
func main() {
panic(errors.New("This is a call!"))
}
results in a 1.2MiB binary on Linux AMD64 and a 1.1MiB binary on Darwin AMD64
What did you expect to see?
Running go vet
or go test
should flag such calls and make suggestions. I expected this
fmt.Errorf has no formatting directives, please instead use "errors.New"
What did you see instead?
Importing the fmt package unnecessarily results in that bloat, and we should do our best to avoid this problem. Unfortunately even our standard library has 374 such superfluous calls if I run
git grep 'fmt.Errorf.\`\|fmt.Errorf."' | grep -v "%"
with the results in the depressible details below
At Orijtech Inc we've got a patch to add this functionality for Go1.19