How to Format Golang Project

Wednesday, December 23, 2020 • One minute to read


Golang is a strictly typed language. It tells you hw to write clean code and it takes care of formatting.
Unlike many languages where formatting style is opinionated, in Golang it is defined by design.


Golang CLI command fmt is used to format the file or files.

Format 1 file

Open the terminal in the golang project.
Run the below command.

1go fmt main.go

Format all the files in the folder

Use ./{folder name} notation to give a particular folder.

1go fmt ./domain

To format the root folder or where terminal is opened.

1go fmt .
5go fmt ./
9go fmt

If you don't pass any argument then it formats the current directory.

Format all the folders recursively

Use ./... special syntax to format the complete project.

1go fmt ./...

Final Words

If you're using git then create a makefile or a bash file to format it before committing the code.


PreviousHow to Remove Consecutive Characters From a String

NextCheck if Key Exists in Map in Golang