Big size of HelloWord executable

291 views
Skip to first unread message

Недовольный клиент

unread,
Apr 13, 2010, 6:40:44 AM4/13/10
to golang-nuts
HelloWord.go compiled in 700 kb executable file.

Is there a way to reduce the size of the executable file?

chris dollin

unread,
Apr 13, 2010, 6:46:25 AM4/13/10
to Недовольный клиент, golang-nuts
2010/4/13 Недовольный клиент <suka...@gmail.com>

HelloWord.go compiled in 700 kb executable file.

Is there a way to reduce the size of the executable file?

Suppose there weren't. Would it matter?

Chris

--
Chris "allusive" Dollin

Rui Vieira

unread,
Apr 13, 2010, 7:01:48 AM4/13/10
to Недовольный клиент, golang-nuts
It's my understanding that the resulting executables are statically linked (which you can check by yourself doing 'ldd 8.out'), hence the "bigger" size.


2010/4/13 Недовольный клиент <suka...@gmail.com>
HelloWord.go compiled in 700 kb executable file.

Is there a way to reduce the size of the executable file?


--
To unsubscribe, reply using "remove me" as the subject.

peterGo

unread,
Apr 13, 2010, 12:50:41 PM4/13/10
to golang-nuts
Yes, there is a way.

First take a look at the classic Hello, World program in Go.

package main

import "fmt"

func main() {
fmt.Printf("Hello, 世界\n")
}

To print the message, it uses the Printf function of the Go fmt
package. If you take a look at the documentation and source code for
the Go fmt package, you will see that it's doing a lot of things to
implement general formatted I/O. fmt, in turn, uses other Go packages
-- "bytes", "io", "os", "reflect", "strconv", and "utf8" -- which may,
in turn, use other Go packages, and so on. After all this code is
compiled and linked, I get an executable file of 721.1 KB (738,380
bytes). Of course, in a larger program, these package functions would
be shared and used many times by many functions.

Since the formatting and printing requirements are very simple in this
case, it's possible to cheat and write the message directly, bypassing
the fmt package and replacing it with the smaller syscall hardware and
OS dependent package.

package main
import "syscall"
func main() {
syscall.Write(0,[]byte("Hello, 世界\n"))
}

After this code is compiled and linked, I get an executable file of
124.1 KB (127036 bytes).

Another way to cheat is to use the temporary bootstrap function
println, which is not guaranteed to stay in the Go language.

package main
func main() {
println("Hello, 世界\n")
}

After this code is compiled and linked, I get an executable file of
121.6 KB (124473 bytes).

Peter

Reply all
Reply to author
Forward
0 new messages