When writing programs which utilize network, you can see quite noticeable slowdown of compilation with CGO_ENABLED=0
.
For example, the simplest HTTP server:
package main
import (
"flag"
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi! glad you requested %s.
", r.URL.Path[1:])
}
func main() {
port := flag.Int("port", 9000, "")
flag.Parse()
http.HandleFunc("/", handler)
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
if err != nil {
log.Fatal(err)
}
}
the timings are:
% time go build
go build 0.46s user 0.06s system 131% cpu 0.396 total
% time CGO_ENABLED=0 go build
CGO_ENABLED=0 go build 3.93s user 0.15s system 143% cpu 2.849 total
So far I'm not using bindings to C so CGo handling or not seems irrelevant, what I'd like to do is to compile 100% static binary, but not if there's such slowdown.
What is the cause of such behavior?