Caching and vendoring improvements

In order to see the improvements we can make with built and cached assets, let's build a project with a third-party dependency.  Prometheus [https://prometheus.io/] is a popular time-series database (also written in Go) that is commonly used for metrics gathering and collection. We may want to start up a Prometheus metrics server in any of our applications in order to learn more about our current running binary, from a systems perspective. To do this, we can import the Prometheus library as follows:

package main
import (
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
http.Handle("/promMetrics", promhttp.Handler())
http.ListenAndServe(":1234", nil)
}

After we instantiate our prometheus server in a basic binary, we can build our binary and execute it. To perform a force rebuild of packages that are already up to date, we can use the -a flag with go build. If you're curious as to what's taking forever in our super long build time, you can also add the -x flag – it'll give you a very verbose output as to what's happening during the build process. 

By default, newer versions of Golang will define a GOCACHE. You can see where it's located using go env GOCACHE. Using a combination of GOCACHE and mod vendor, we can see that our build time has significantly improved. Our first build in the list is a cold build, forcing packages to be rebuilt so they're up to date. Our second build, which has some items stored from the mod vendor stanza, is much quicker. Our third build, which should have most build elements cached, is very quick in comparison. The following screenshot illustrates this:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.20.224.107