You can do that in one of these two ways:
-
For Goland: preferences ->Tools ->File Watchers and enable
go fmt
. This way on each saves it will format the file. -
once your code is done. just run the command
gofmt -s -w .
in the terminal in the required directory or else in the needed file. it will format your whole directory/file as per your need.