5 Helpful utilities in Go toolchain
I bet you would have never come across such a complete toolchain in any language or framework. There are so many utilities that Go natively provides, if you start exploring them you cannot finish. Actually, you can, once you fall in love with Go.
Following are the 5 interesting and helpful utilities from Go toolchain that I have discovered. Feel free to add more in comments! Let's start.
1. Documentation (doc)
This command lets you see the documentation of any Go’s package from your terminal. It saves a lot of time!go doc <package_name>
Wait, there is more to it. You can even see the documentation of a particular methodgo doc <package_name>.<method_name>
2. Module Maintenance (mod)
Assuming you already know what are go modules, this is the goto command to manage your project dependencies with granularity. I will pinpoint few of the commands which have proved useful to me, you can checkout out all available commands using go mod help
vendor
Allows you to build the vendor folder using your go modules filego mod vendor
tidy
Adds missing and remove unused modules go mod tidy
download
It downloads your module to the local cache. This helps when you are building your project with large size external packages. It’s better to cache them which certainly helps increasing build speed. go mod download <package_name>
3. Formatting (fmt)
The all-time favorite tool of everyone! With just one command you can format (indentation, renaming etc) your whole Go code. If you are using VSCode with Go plugin installed, then every time you save your code, go fmt is run automatically. go fmt
and gofmt
are two different commands used to format Go files. Interestingly, go fmt internally calls gofmt only.
To format a filego fmt <file_path>
To format the whole directory go fmt .
gofmt can make your work easier. It has multiple flags to enable you to format your code with better visibility. One of the flags helps to see the diff between your original source code and formatted code.
gofmt -d <filepath>
Do not print reformatted sources to standard output. If a file’s formatting is different than gofmt’s, print diffs to standard output. Use this go doc cmd/gofmt
to read more.
4. Build (build)
For those who are writing production-level code are using this command to its best level. However, those who are yet not writing production-level code are just using go build
to create the binary. Believe me, build tool is something every framework strive to get included in their native processing and Go has done it beautifully.
Race Detection
go build -race
enable data race detection while building project binary only and provides you details about where the program can run into race conditions. Extending this, race flag can be used with go test
and go install
as well.
X Flag
If you are curious about how Go is performing the build process, go build -x
allows you to see all invocations done by Go compiler. This command helps to debug the process when you are doing a conditional build.
Cross Compile
GOOS and GOARCH env variables point to the target system platform and architecture. By default, they points to the current system’s platform. go build
uses these configurations to generate project binary. By changing these env variables, we can generate binaries for different types of machine.
Example:export GOARCH=”amd64"
export GOOS=”linux”
go build
To read more, go help build
!
5. Profiling
This is the most powerful tool that Go provides natively. We all owe to Go’s engineers for coming up with this utility. Right from diagnosing the latency problems to finding bottlenecks with heap and threads in the code, Go profiling tools is something a production-grade system wants!
go tool trace
trace lets you collect your program traces and helps to visualize it. You can plugin your custom traces as well. Read more here.
go tool pprof
pprof lets you collect CPU profiles, traces, and heap profiles for your Go programs. Read more here.