5 Helpful utilities in Go tool for developers

5 Helpful utilities in Go tool for developers

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 method

go doc <packagename>.<methodname>

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 file

go 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 file

go 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.

_________________________________________________________________________________

You may also like: Google’s Go Essentials For Node.js / JavaScript Developers

_________________________________________________________________________________

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.

Further Reading

How to use the Go Dev container for Visual Studio Code

Google’s Go Essentials For Node.js / JavaScript Developers

Moving from NodeJS to Go

Originally published by Yashish Dua at medium.com

=============================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

web-development go

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

5 Web Design Software You Should Use for Web Development

Want to know best web design software to built an awesome website? We've handpicked the top 5 web development software you should use for your project!