On Mon, Aug 8, 2016 at 5:12 PM, Ian Lance Taylor <i...@golang.org> wrote:

> On Sun, Aug 7, 2016 at 7:08 PM, Amit Saha <amitsaha...@gmail.com> wrote:
> >
> > From an old thread [1], I learned that CGO_ENABLED=0 will create static
> > binaries for programs using the net/http package. I still see the same
> > behavior as of go version go1.6.3 linux/amd64. Does this mean this is the
> > only way to get statically linked binaries for programs using net/http?
>
> It is a simple way that works.  Why do you want another way?
>
> The issue is not actually the net/http package, it's the net package.
> The net package is designed to fall back to the C library for DNS
> lookups in some cases.  Setting CGO_ENABLED=0 disables that fallback,
> and the net package will use pure Go code for DNS (which it does in
> most cases anyhow).
>

If I am not mistaken, you can achieve the same with the somewhat clearer
"netgo" build tag:

$ cat main.go
package main

import (
"net"
"log"
)

func main() {
c, err := net.Dial("tcp", "localhost:4000")
if err != nil {
log.Fatal(err)
}
defer c.Close()
c.Write([]byte("hello"))
}

$ go build -tags=netgo -o netgo main.go
$ go build -o netcgo main.go

$ ldd netcgo
linux-vdso.so.1 (0x00007ffc8c729000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f6cb60bd000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007f6cb5d1f000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6cb62da000)

$ ldd netgo
  not a dynamic executable

-s

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to