Back

Go Vet Command and Its Usage

The Golang vet command is a built-in tool that is included in the Go programming language. It is designed to help developers identify and diagnose potential problems in their Go code. This can be useful for catching errors before they are compiled, ensuring that the code is as clean and efficient as possible.

Go vet command is a handy tool that can help you write better and cleaner code. It helps you find errors in your application and reports suspicious constructs such as abnormal or useless code, as well as detect common mistakes using different packages.

Vet command (or should we say vet commands?) is a collection of different analysis tool commands and sub-analyzers. It is worth mentioning that vet uses heuristics which can’t guarantee that 100% of the reports will be genuine issues. However, it can detect errors that compliers might omit.

Using the vet command

You can invoke vet with the go command. The following command will vet the current directory package:

go vet

If you wish to vet a package in another directory you need to provide a path:

go vet my_machine/project/...

If you want to vet specific packages you can use:

go help packages

Listing all available checks with tool vet help

You can list all available checks by running the:

go tool vet help

Now, let’s briefly go over what you can exactly get from the go tool vet help commands.

asmdecl

asmdecl reports mismatches between the Go declarations and assembly files.

assign

This is a check for useless assignments.

atomic

This is for common mistakes using the sync/atomic package and will check any irregularities in the usage of the atomic function.

bools

Detects common mistakes that involve boolean operators.

buildtag

Checks the form and location of the +build tags. The +build flags supported by go vet are related to the resolution and execution of the control package.

cgocall

Checks for violations of the cgo pointer passing rules.

composites

Looks for any composite literals using unkeyed fields.

copylocks

Detects copied locks passed by value as an error. The locks should never be copied, as the copy of the lock will copy the internal state of the “original” making it the same as the “original”.

httpresponse

Checks for any mistakes in HTTP responses usage

loopclosure

Checks for any references to loop variables from within nested functions.

lostcancel

Detects cancel func that is returned by context.WithCancel. Creating a cancellable context will return a new context with a function that enables you to cancel that particular context. You can use that function to cancel each and every operation linked to that context, however, it has to be called in order. Otherwise, it will leak context.

nilfunc

Searches for useless comparisons between nil and functions.

printf

Checks for consistency in Printf format. Constructs such as Printf calls with arguments that don’t align with the format string will be flagged.

shift

This go vet analysis tool will try to find shifts that either exceed or equal the integer width.

stdmethods

Checks for methods that you have implemented from the standard library interfaces and their compatibility.

structtag

Detects fields that don’t conform to the convention defined in the reflect package.

tests

This analyzer catches likely mistakes made while using either a test or an example.

unmarshal

It returns all instances of passing non-interface or non-pointer values to unmarshal.

unreachable

Simply checks for any code that is unreachable.

unsafeptr

Detects any invalid conversion of uintptr to unsafe.Pointer.

unusedresults

Detects unused results of calls to functions.

Conclusions on the go vet analysis tool

Go vet is a very useful collection of tools that can help you find different issues with your source code. By design, all checks are performed, unless any flags are set to true. In that case, only those tests will be run. Analogically, setting a flag to false will disable that particular test.

Please mind that not all returned errors are genuine problems. Moreover, if you wish to look for other subtle issues in your go programs, one that no specific checker detects, there is an option to add additional checks and custom analyzers.

 

Golang (or Go in short) is a breath of fresh air in the coding market. A long-needed shakeup in the stale programming market with a cute gopher as a mascot. Its development was started in 2007 by designers Robert Griesemer, Rob Pike, and Ken Thompson.

Written by Yanick

To a degree, we’ve talked about SaaS solutions and their importance in our recent articles. Today, the focus is on security tools offered as cloud services and how they can benefit your company no matter the scope.

Written by Yanick

You can of course build a REST API by yourself, but frameworks are powerful tools, built to offer a user simplified ways of doing things, in this case: REST API. A framework is essentially a tool built for one purpose with features and libraries.  As it’s pre-built you can also be sure that it works […]

Written by Ilona

Garbage collector algorithms are mechanisms used in programming languages and runtime environments to automatically manage memory by identifying and reclaiming unused or unreachable objects. They are employed to manage memory on the go without the developer’s constant interventions.

Written by Yanick

The cloud offers plenty of opportunities both in terms of improving business efficiency and reaching end customers. If you’re gearing up for cloud-native application development, take a few minutes to learn about good practices to follow and pitfalls to avoid.

Written by Ilona