
Ask HN: How do Go and Rust binary file sizes compare for the same app? - vram22
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.<p>For example: this simple test Go program compiles to an EXE of ~2 MB on Windows 7 with Go 1.9:<p><pre><code>    &#x2F;&#x2F; From: https:&#x2F;&#x2F;gobyexample.com&#x2F;reading-files

    package main
    import (
        &quot;bufio&quot;
        &quot;fmt&quot;
        &quot;io&quot;
        &quot;io&#x2F;ioutil&quot;
        &quot;os&quot;
    )

    func check(e error) {
        if e != nil {
            panic(e)
        }
    }

    func main() {

        dat, err := ioutil.ReadFile(&quot;dat&quot;)
        check(err)
        fmt.Print(string(dat))

        f, err := os.Open(&quot;dat&quot;)
        check(err)

        b1 := make([]byte, 5)
        n1, err := f.Read(b1)
        check(err)
        fmt.Printf(&quot;%d bytes: %s\n&quot;, n1, string(b1))

        o2, err := f.Seek(6, 0)
        check(err)
        b2 := make([]byte, 2)
        n2, err := f.Read(b2)
        check(err)
        fmt.Printf(&quot;%d bytes @ %d: %s\n&quot;, 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(&quot;%d bytes @ %d: %s\n&quot;, n3, o3, string(b3))
        
         _, err = f.Seek(0, 0)
        check(err)

        r4 := bufio.NewReader(f)
        b4, err := r4.Peek(5)
        check(err)
        fmt.Printf(&quot;5 bytes: %s\n&quot;, string(b4))

       f.Close()
    }</code></pre>
======
vram22
I'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
There'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-
program...](https://www.rust-lang.org/en-US/faq.html#why-do-rust-programs-
have-larger-binary-sizes-than-C-programs)

~~~
vram22
Thanks.

>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.

