DEV Community

Artem Tanyhin
Artem Tanyhin

Posted on

Formatting and linting in Go ๐Ÿ“œ

Hey there! Thanks for checking in again! This week, I've been trying to set up formatting and linting for my SSGo project. Maybe you'll get some valuable information from my experience!

Formatter

Unlike many languages, Go actually has a built-in formatter called gofmt. You can run gofmt -s -w . in order to format all files in current directory.

But, unlike many formatters for other languages, Go actually has a specific pre-defined setting for all files. This was actually Go development team's standpoint to have all Go code look the same, so developers aren't confused by formatting differences.

Linter

Unlike gofmt, Go does not have a built-in linter. It does have go vet command, which checks for checking actual errors in code, but most of the times it isn't enough to keep the project clean.

There is however a tool called golangci-lint that does a great job, however it needs to be downloaded separately.

Being a JS/TS developer, I am used to being able to add dependencies/tools via package managers, and having to download a linter separately isn't very convenient for the contributors.

But other than that, using golangci-lint run command did a great job, and all of the settings for the linter can be defined in .golangci.yaml file.

VS Code integration

Before I saw any of the separate tools, I was using an official Go extension for VS Code under the code of golang.go. It was formatting and linting all my code for me, so I included it to be the formatter in settings.json and set the linter as "golang-ci".

This allows for automatic formatting on save and linting, and is much more convenient than having to download linter and run it in CLI manually.

Summary

When I added everything and wrote a proper CONTRIBUTING.md file, I squashed everything into one commit and pushed.

There wasn't much to fix to pass the linter, and the formatter simply changed spaces to tabs in variable definition.

If you are a Go developer and know any better solutions then what I have done, please leave a comment. In the end, we are always learning.

Top comments (0)