How to Format Golang Project

Wednesday, December 23, 2020 • One minute to read

Golang2020to-the-pointgolang

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.

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 .
2
3OR
4
5go fmt ./
6
7OR
8
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.


Golang2020to-the-pointgolang

PreviousHow to Remove Consecutive Characters From a String

NextCheck if Key Exists in Map in Golang