Okewole Oluwatobi * learning to become a Back-end engineer

The best Go tools for a better web development experience

3 min read 1010

Golang tools for easier web development

Software development doesn’t have to be clunky or cumbersome. In fact, most modern programming languages have various tools to ensure that developers who interact with them have a seamless experience.

In this article, we’ll look at some of the most useful tools that provide a better development experience when coding in Go. Go provides us with several amazing tools, as well as a package we can use to write custom Go tools that fit our needs.

Prerequisites

To follow along with this tutorial, you will need:

Understanding the Go command

To get started with Go tools, we have to first learn about the go command, which is used when executing a Go tool. When you install any Go distribution, you will have access to the go keyword.

According to Rob Pike, one of the creators of Go, “The purpose of the new go command is our return to this ideal, that Go programs should compile without configuration or additional effort on the part of the developer beyond writing the necessary import statements.”

When working with Go tools, the syntax for the execution command is usually the name of the tool appended to the go command, such as go fix. To see a list of all known tools, simply run the go tools command in your terminal.

Improved workflow with Go tools

In this section, we’ll look at various Go tools that can improve our development workflow:

1. go vet

Errors — either syntactic, semantic, or logical — are bound to occur when writing code in any programming language. The go vet tool checks Go code for any syntactic errors and reports them. This tool is especially handy when you’re working with a team.

When the go vet command is run, it checks for errors within the code in the path it was called for. The go vet can also be called with a path like go vet home/src/google-clone, where it would check the specified path for errors.

One of the most powerful features of the go vet tool is that it provides a list of checks we can specify. When a check is specified, the Vet tool will only report instances where our code does not obey the rule of the specified check.

We made a custom demo for .
No really. Click here to check it out.

For example, if we run the command, go vet -tests=true, the Vet tool will check for any errors in the test files of that directory. To see a full list of all the checks that can be specified, run the go tool vet help command.

2. gofmt

The gofmt tool is one of the most popular tools in the Go ecosystem. Its function is to format Go source code. The gofmt tool favors tabs over spaces and replaces any instance of indentation in your code base with tabs. There are a few flags you can specify to further extend the functionality of the gofmt tool:

package main

import "fmt"

func main() {
   s := []string{"foo", "bear", "bar", "bare"}
   v := s[1:len(s)]
   for x, _ := range v {
      fmt.Println(x)
   }
}

The code snippet above ranges over a slice and prints out the index of each element in that slice. If we want to format it, we simply run this command : gofmt -w -s .

The command includes the w flag, which tells the gofmt tool to overwrite the default formatting of our file and not to print out the differences made. The s flag we specified makes the gofmt tool check for certain ambiguity in our code and simplify it.

When we run the above command, our code will be refactored as seen below.

func main() {
   s := []string{"foo", "bear", "bar", "bare"}
   v := s[1:]
   for x := range v {
      fmt.Println(x)
   }
}

The slice and range expression were simplified. Amazing, right? The gofmt tool should be a friend of every Go developer.

3. GoDoc

A quick look at the source code of your favorite Go package will tell you how seriously documentation is taken within the Go ecosystem. The GoDoc tool is used to generate documentation for Go source code based on comments written within the code. This generated documentation can then be displayed as either HTML — specified by using the -html tag — or plain text.

package main

import "fmt"
//Copyright 2020
//This is a sample documentation for a code snippet.
/*
   This is the main function, it contains a slice,
   and ranges over the slice.
*/
func main() {
   s := []string{"foo", "bear", "bar", "bare"}
   v := s[1:]
   for x := range v {
      fmt.Println(x)
   }
}

The snippet above is the commented version of the range example discussed above. If we run godoc -http=:6060 and visit localhost:6060 on the browser, we would see an HTML page like this, which shows us our documentation couple with our code.

Godoc HTML

GoDoc also provides us with the functionality to view the documentation of any package available in the Go standard library locally and without internet connection. To view the documentation of a package, simply append the name of the package to godoc and run the command.

4. go get

The Get tool, as the name implies, is used for fetching and installing third party packages or dependencies into Go projects. The go get command takes in the URL of the package to be installed.

Developers can create third-party Go tools that are open source. We can also install these third-party tools using the go get command, passing in the URL to the package like go get github.com/kisielk/errcheckgithub.com/kisielk/errcheck. The go get tool is by far the most important and commonly used Go tool.

Conclusion

All of these Go tools can be used in combination with Git hooks to automatically execute them before they are committed with version control. This article is a great guide to get you started with Git hooks.

In this blog post, we covered how to leverage various Go tools to improve our development workflow and overall experience while writing code. To learn more about the various Go tools, visit the Go website.

You come here a lot! We hope you enjoy the LogRocket blog. Could you fill out a survey about what you want us to write about?

    Which of these topics are you most interested in?
    ReactVueAngularNew frameworks
    Do you spend a lot of time reproducing errors in your apps?
    YesNo
    Which, if any, do you think would help you reproduce errors more effectively?
    A solution to see exactly what a user did to trigger an errorProactive monitoring which automatically surfaces issuesHaving a support team triage issues more efficiently
    Thanks! Interested to hear how LogRocket can improve your bug fixing processes? Leave your email:

    : Full visibility into your web apps

    LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

    In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.

    .
    Okewole Oluwatobi * learning to become a Back-end engineer

    Leave a Reply