Skip to content
Related Articles

Related Articles

Auto Format Go Programming Language Source Code with gofmt

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 11 May, 2022

Formatting of source code adds an important feature to the quality and readability of the code. In Golang, we have built-in packages and commands to auto-format code in the source file as per the standards and best practices. We can use the gofmt command in the CLI to autoformat a Golang source file. We can autoformat the go code with gofmt package in Golang. Using the command gofmt command from the terminal or command line, we can format the go source file.

Using go fmt 

We can use the go fmt command in the gofmt package in Golang to automatically format the go source file. The go fmt is a command-line utility in Golang which allows us to format go source files as per standard semantics and convention. 


package main
import (
func main() {
    var word string
    fmt.Printf( "Enter any string: ")
    fmt.Scan( &word)
    num :=6
    fmt.Println("The number is : ", num )


Here, we had a file, a go source file with inconsistent syntax and not styled properly, by using the go fmt utility from the command line, we are able to get a formatted and nicely structured go code in the provided file. The source file had a few inconsistent uses of spaces in the assignment operator and inside the parenthesis i.e. ( “”) or (“” ).  After the command has been executed, the improper and inconsistent formatting has been corrected and the code looks a bit cleaner.

go fmt filename.go


package main
import (
func main() {
    var word string
    fmt.Printf("Enter any string: ")
    num := 6
    fmt.Println("The number is : ", num)

Here, we can see the go fmt command is used by parsing a parameter as the go source file which we need to format properly. The go fmt command will fix up the indentations, the semantics of operators, or any other syntactic inconsistencies in the file automatically. 

This looks quite a small thing from the example point of view, but consider a large project with multiple packages, modules, and source files, this could make your project a lot more readable and consistent.

Store Formatted Code in New file

We can store the formatted code in a new file using the > operator. This operator allows us to define a refactoring file and the original source file to be formatted. 

gofmt original_file.go > output_file.go


As we can see the formatted code is saved in a different file than the original source file.  This helps in making sure the code remains safe after formatting by checking it with go vet filename.go. The go vet command is used to check for pre compilation errors in the source file thereby providing any syntactical errors or checks.

Generating a Diff from the fmt command

We can even generate the diff for the formatted code in a different file using the -d parameter in the fmt command,

gofmt -d original_file.go 


$ gofmt -d main.go
diff -u main.go.orig main.go
--- main.go.orig        2022-04-12 21:53:35.987557100 +0530
+++ main.go     2022-04-12 21:53:35.988557200 +0530
@@ -1,13 +1,13 @@
-package main
-import (
-       "fmt"
-func main() {
-       var word string
-       fmt.Printf("Enter any string: ")
-       fmt.Scan(&word)
-       num := 6
-       fmt.Println("The number is : ", num)
+package main
+import (
+       "fmt"
+func main() {
+       var word string
+       fmt.Printf("Enter any string: ")
+       fmt.Scan(&word)
+       num := 6
+       fmt.Println("The number is : ", num)

This will generate the output as a diff in the terminal. It won’t change the format in the source file. A diff file is basically a format to store the changes in the source file with a timestamp and file metadata. To save changes of the diff in the source file into another diff file, we can store it using the  > operator.

gofmt -d original_file.go > diff.go


Now, we have uses the -d option in the gofmt command to create a file diff.go that contains all the diff of the formatted code in the source file. So, here the changes in the code formating are stored in a separate file as mentioned in the command after the > operator. This command appends the output of the command in the mentioned file in this case it’s diff.go or it can be any other file you like. Thus, we can generate the diff of the format code changes in a separate file. 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!