
With gofmt, you can format Go source code in the opened file or in the whole Go project.įor a single file, you can use the built-in import management and code formatter. To customize import management, open settings by pressing Ctrl+Alt+S and navigate to Go | Imports. Comparing to gofmt, GoLand’s formatter works with syntactically incorrect code and can be invoked on an arbitrary block. There are other features such as automatically inserting semicolons, wrapping parameters and arguments, and others.Īlso, you can use On code reformat option ( Editor | Code Style | Go, click Other tab). like this : 'faultFormatter': 'esbenp. Choose a default formatter instead of null. If your file is being properly formatted without any issues, it means there is something wrong in formatOnSave settings. This option lets you invoke both formatters by pressing Ctrl+Alt+L whenever you want.Īlternatively, use the Reformat code option in Actions on Save. Try to format your code manually by pressing Ctrl + Shift + P to open Command Palette and select Format Document. This action is enabled by default for GoLand. When triggered by pressing Ctrl+S, the IDE runs the built-in GoLand formatter and gofmt. To format code in the opened file, go to Tools | Go Tools | Go fmt file. To format code in the current project, go to Tools | Go Tools | Go fmt project. Do not autoformat code on save - undesirable. Disable goimports and move to gofmt - so-so. Add new package manually with dep ensure -add packagename B. To format code before you commit your changes into VCS, select the Go fmt checkbox. In order to run dep ensure I have to save a file goimports finds unused package and removes it from imports. For more information about committing your changes, see Commit and push changes to Git repository.įor more information about gofmt, see Command gofmt.

#GOLAND GOFMT ON SAVE UPDATE#
With goimports, you can automatically update your Go import lines (add missing and remove unreferenced imports). If your project does not have goimports, click the go get goimports link in the Goimports file notification window.
#GOLAND GOFMT ON SAVE INSTALL#
Otherwise, open the Terminal tool window ( View | Tool Windows | Terminal), and type the following command to install goimports: go get /x/tools/cmd/goimports. To add missing imports, navigate to Tools | Go Tools and click Goimports file.

The goimports tool also formats your code in the same style as gofmt. Visual Studio Code, or GoLand, you can set up the editor to automatically run. For more information about goimports, see Command goimports. GOAL: Run: gofmt -w calculator.go This will reformat the file in place.
