Auto Format Go Programming Language Source Code with Gofmt


Let's look at how ‘gofmt’ can automatically style your Go source code in a consistent manner so that it is easier to understand and manage. In order to write readable, maintainable code, it must be formatted correctly. Go source code can be formatted using the 'gofmt' command-line tool. Using a set of rules and conventions, this programme automatically reformats your Go code to make it simpler to read and comprehend.

What is Gofmt?

A command-line tool called Gofmt is used to format Go source code consistently. Your code's space, indentation, and line breaks are automatically adjusted to make it easier to understand and manage. Every Go installation comes with the gofmt tool since it is a component of the standard Go toolchain.

Why Use Gofmt?

Using ‘gofmt’ to format your code has several benefits −

  • Consistent formatting − ‘Gofmt’ enforces a consistent style and format for your code, making it easier to read and understand.

  • Easier code reviews − Consistent formatting makes code reviews more straightforward since reviewers can focus on the logic and structure of the code rather than the formatting.

  • Saves time − Manually formatting code can be time-consuming, especially for larger projects. Using ‘gofmt’ automates this process and saves you time.

How to Use Gofmt

Using ‘gofmt’ is simple. Open a terminal or command prompt and navigate to the directory containing your Go source code. Then, run the following command −

gofmt -w .

This command reformats all the Go source code files in the current directory and its subdirectories.

The -w flag tells ‘gofmt’ to write the changes back to the source files. If you omit this flag, ‘gofmt’ will simply print the reformatted code to the standard output.

If you want to check whether your code complies with gofmt's formatting rules without actually modifying it, you can use the following command −

gofmt -d .

This command displays the differences between the formatted code and the original code without changing the source files.

Customizing Gofmt

You can modify the behaviour of the tool using a configuration file if you prefer a different formatting aesthetic than what is provided by the ‘gofmt’ rules by default. A set of guidelines for formatting code are defined in the configuration file, which is a Go source file. Then, to put these rules into effect, run gofmt with the -r flag.

An example of a ‘gofmt’ configuration file is shown below −

// .gofmt.go
package main

import "golang.org/x/tools/imports"

func main() {
   // Customize the imports format
   imports.LocalPrefix = "github.com/myproject"

   // Customize the tab width
   imports.TabWidth = 4
}

To apply these rules, run the following command −

gofmt -r .gofmt.go 

Conclusion

Writing clean, readable, and maintainable Go code requires the use of ‘gofmt’. You may save time, increase consistency, and make your code simpler to read and comprehend by automating the formatting of your code. You may rapidly reformat your Go code to adhere to the common formatting standards for Go or your individual formatting rules with a few straightforward commands.

Updated on: 06-Apr-2023

252 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements