There are two things we need to keep in mind when compiling a Go program
- Go Build
- We can build the code wrt the host system we gonna deploy, even though we’re using a windows machine, we can build code for the mac, by either giving the system as env or adding a special files in the code called
FILE_GOOS.go/FILE_GOARCH.go/FILE_GOOS_GOARCH.go
- We can build the code wrt the host system we gonna deploy, even though we’re using a windows machine, we can build code for the mac, by either giving the system as env or adding a special files in the code called
- Go File tags
- In code we can control some code blocks to build in certain way, using file tags like
// +build os1[,arch] [os2[,arch2]]// +build !linux,!darwin !cgo// +build ignore// +build custom-tag
- In code we can control some code blocks to build in certain way, using file tags like
We have a $GOPATH variable being exported by Golang, which tells us the location where the Golang is installed and stores it’s things. when we open the GOPATH we will find three folders bin, pkg, src(for versions before 1.16), now we find only two things bin and pkg. The src is moved to pkg/mod folder, this is called module mode
Note: when we’re using GVM(Go version manager) this things change, as GVM have separate path things running and it’s keep the packages isolated for different versions
GO Proxy:
- The go proxy is like a proxy which is generally set to
https://proxy.golang.org,directthat can be found via runninggo env | grep "GOPROXY"this is responsible to download packages from internet, when we run go get command it will check the proxy database and if it not find the package in that then it will make the network call to the internet. one advantage of this is, we can download packages from the proxy even if the GitHub(or other hosts like gitlab.com, go.uber.org) is down - When a package code is present in the local cache, Go will verify its integrity by comparing the checksum in
go.sumagainst the authoritative checksum database (GOSUMDB, likesum.golang.orgviaproxy.golang.org). If the checksums match, Go uses the locally cached package. If the checksums don’t match or the version isn’t cached, Go downloads the required version from the internet - When Go downloads a new version of a package, it stores it in a separate, immutable and read-only directory within the module cache (
$GOMODCACHE/module@version/). The old cached version is left untouched and remains in the cache. Both versions coexist simultaneously, allowing different projects to use different versions of the same module without conflicts
GO SumDB:
- When we download a package then it will verify the checksum of the package from
https://sum.golang.orgthat can be found via runninggo env | grep "GOSUMDB", if the checksum doesn’t match then the Golang will halt the operation bygo.sum