Is there a way to reduce the size of the executable file?
HelloWord.go compiled in 700 kb executable file.
Is there a way to reduce the size of the executable file?
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.
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