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.
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>
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
Allows you to build the vendor folder using your go modules file
go mod vendor
Adds missing and remove unused modules
go mod tidy
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>
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.
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.
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.
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.
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.
To read more,
go help build!
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.
Originally published by Yashish Dua at medium.com
With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...
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...
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.
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.
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!