Linking Go Programs
In this lesson, you'll learn about linking the Go programs.
We'll cover the following
To build programs, we need to link these packages. The go build command builds the packages and links them into an executable. In this lesson we will examine this process. We will also take a pick into the idea of embedding extra information into Go programs.
Linking Go programs
The go build command linked our multi-git program, too. Linking means taking all the packages that were built or cached (the pkg.a files) and combining them into a single executable. First, some debug information is created because it’s a debug build.
## # github.com/the-gigi/multi-git
#
mkdir -p $WORK/b001/
cat >$WORK/b001/_gomod_.go << 'EOF' # internal
package main
import _ "unsafe"
//go:linkname __debug_modinfo__ runtime.modinfo
var __debug_modinfo__ = "0w\xaf\f\x92t\b\x02A\xe1\xc1\a\xe6\xd6\x18\xe6path\tgithub.com/the-gigi/multi-git\nmod\tgithub.com/the-gigi/multi-git\t(devel)\t\ndep\tgithub.com/fsnotify/fsnotify\tv1.4.7\th1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=\ndep\tgithub.com/hashicorp/hcl\tv1.0.0\th1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=\ndep\tgithub.com/magiconair/properties\tv1.8.0\th1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=\ndep\tgithub.com/mitchellh/go-homedir\tv1.1.0\th1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=\ndep\tgithub.com/mitchellh/mapstructure\tv1.1.2\th1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=\ndep\tgithub.com/pelletier/go-toml\tv1.2.0\th1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=\ndep\tgithub.com/spf13/afero\tv1.1.2\th1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=\ndep\tgithub.com/spf13/cast\tv1.3.0\th1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=\ndep\tgithub.com/spf13/cobra\tv1.0.0\th1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=\ndep\tgithub.com/spf13/jwalterweatherman\tv1.0.0\th1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=\ndep\tgithub.com/spf13/pflag\tv1.0.3\th1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=\ndep\tgithub.com/spf13/viper\tv1.4.0\th1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=\ndep\tgolang.org/x/sys\tv0.0.0-20190215142949-d0b11bdaac8a\th1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=\ndep\tgolang.org/x/text\tv0.3.0\th1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=\ndep\tgopkg.in/yaml.v2\tv2.2.2\th1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=\n\xf92C1\x86\x18 r\x00\x82B\x10A\x16\xd8\xf2"
EOF
Then, a huge import configuration list is prepared. Here is a small fraction:
cat >$WORK/b001/importcfg << 'EOF' # internal
## import config
packagefile fmt=$WORK/b002/_pkg_.a
packagefile github.com/the-gigi/multi-git/cmd=$WORK/b032/_pkg_.a
packagefile runtime=$WORK/b005/_pkg_.a
EOF
cd /Users/gigi.sayfan/git/multi-git
/usr/local/go/pkg/tool/darwin_amd64/compile -o $WORK/b001/_pkg_.a -trimpath "$WORK/b001=>" -p main -lang=go1.13 -complete -buildid 0EL3XlNtNsVIvACzxsfo/0EL3XlNtNsVIvACzxsfo -goversion go1.14.2 -D "" -importcfg $WORK/b001/importcfg -pack -c=4 ./main.go $WORK/b001/_gomod_.go
/usr/local/go/pkg/tool/darwin_amd64/buildid -w $WORK/b001/_pkg_.a # internal
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile github.com/the-gigi/multi-git=$WORK/b001/_pkg_.a
packagefile fmt=$WORK/b002/_pkg_.a
packagefile github.com/the-gigi/multi-git/cmd=$WORK/b032/_pkg_.a
packagefile runtime=$WORK/b005/_pkg_.a
packagefile errors=$WORK/b003/_pkg_.a
...
packagefile vendor/golang.org/x/crypto/poly1305=$WORK/b116/_pkg_.a
packagefile vendor/golang.org/x/sys/cpu=$WORK/b117/_pkg_.a
packagefile vendor/golang.org/x/text/transform=$WORK/b123/_pkg_.a
EOF
Finally, the executable can be linked using the linker (/usr/local/go/pkg/tool/darwin_amd64/link
):
mkdir -p $WORK/b001/exe/
cd .
/usr/local/go/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=j5RvisPjFj3MWPk1cui-/0EL3XlNtNsVIvACzxsfo/0EL3XlNtNsVIvACzxsfo/j5RvisPjFj3MWPk1cui- -extld=clang $WORK/b001/_pkg_.a
/usr/local/go/pkg/tool/darwin_amd64/buildid -w $WORK/b001/exe/a.out # internal
mv $WORK/b001/exe/a.out multi-git
Now that, we took a look behind the curtain, you should be happy that Go is nice enough to take care of it for you and doesn’t require that you perform all these steps.
Get hands-on with 1400+ tech skills courses.