Skip to content

How to Format Golang Project

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.

Formatting

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.

go fmt main.go

Format all the files in the folder

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

go fmt ./domain

To format the root folder or where terminal is opened.

go fmt .

OR

go fmt ./

OR

go 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.

go fmt ./...

Final Words

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