Go program not statically linked using 1.4.2

952 views Asked by At

Trying to build a statically linked version of a go program that runs an http server, and uses the net package to determine and parse the IP address of the incoming request. With this build statement:

CGO_ENABLED=0 go install -a -ldflags '-s' .

And this preamble in my program:

package main

import (
    "encoding/json"
    "errors"
    "fmt"
    "log"
    "net"
    "net/http"
    "path/filepath"
    "strings"

    "golang.org/x/blog/content/context/userip"

    "github.com/oschwald/maxminddb-golang"
)

This worked building with go 1.3, producing a statically linked program, but is not working with go 1.4.2. The build succeeds, but the program is not statically linked.

Does anyone know what is going on?

1

There are 1 answers

0
Mark Larter On BEST ANSWER

More searching turned up this thread in the golang issue tracker about being unable to statically link a program that imports the net package due to changes between 1.3 and 1.4.

Reading down I found this workaround suggested by ianlancetaylor to use the -installsuffix switch to specify cgo. This made my build statement:

CGO_ENABLED=0 go install -a -installsuffix cgo -ldflags '-s' .

Which worked for me. Build succeeded and produced a statically linked program.