3 ms·
Ask HN: How do Go and Rust binary file sizes compare for the same app?
E.g. if you write and compile a simple Unix-like wc (word count) program in both Go and Rust, how do sizes of the resulting binaries compare?
And similarly for a few other apps, to get a rough idea of binary file size differences between them.
For example: this simple test Go program compiles to an EXE of ~2 MB on Windows 7 with Go 1.9:
// From: https://gobyexample.com/reading-files
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
dat, err := ioutil.ReadFile("dat")
check(err)
fmt.Print(string(dat))
f, err := os.Open("dat")
check(err)
b1 := make([]byte, 5)
n1, err := f.Read(b1)
check(err)
fmt.Printf("%d bytes: %s\n", n1, string(b1))
o2, err := f.Seek(6, 0)
check(err)
b2 := make([]byte, 2)
n2, err := f.Read(b2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n2, o2, string(b2))
o3, err := f.Seek(6, 0)
check(err)
b3 := make([]byte, 2)
n3, err := io.ReadAtLeast(f, b3, 2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3))
_, err = f.Seek(0, 0)
check(err)
r4 := bufio.NewReader(f)
b4, err := r4.Peek(5)
check(err)
fmt.Printf("5 bytes: %s\n", string(b4))
f.Close()
}
- vram22 9y agoI've also compiled small programs that I wrote in D and Free Pascal (not equivalent to the program above, but simple command-line utilities), like wc clones or similar, and their size is quite a bit smaller than the Go binary size above, e.g. in the range of 50 to 100 KB (IIRC). And similar programs in C were ~22 KB. I know there can be many reasons for the differences, such as what libraries are included by default, maybe transitive dependencies, etc.
- kbenson 9y agoThere's a Rust FAQ entry on file sizes[1], but it also sort of glosses over that Rust defaults to static compilation. Go does as well, and I believe Go does as well. Part of the size of executables can be attributed to these languages implementing their own libc equivalents (or wrapping libc). The stories probably fairly nuanced as each language has it's own story, but that's the general idea. 1: https://www.rust-lang.org/en-US/faq.html#why-do-rust-programs-have-larger-binary-sizes-than-C-programs https://www.rust-lang.org/en-US/faq.html#why-do-rust-program...
- vram22 9y agoThanks. >Part of the size of executables can be attributed to these languages implementing their own libc equivalents (or wrapping libc). Yes, I agree. Both of them have more features and I guess (I know it a bit for Go, not sure for Rust) do some more / higher level things than plain C, which may partly be why they need to wrap libc (or implement the equivalent) with non-trivial additional code.