go install /x/tools/cmd/goimportslatest In addition to fixing imports, goimports also formats your code in the same style as gofmt so it can be used as a replacement for your editor's gofmt-on-save hook. I’ll describe this problem in more detail in the next session. Overview Command goimports updates your Go import lines, adding missing ones and removing unreferenced ones. A new quick-fix will move //go:build lines to the top of the file, as the new syntax requires. When this option is checked, you can invoke gofmt and GoLand’s built-in formatter with the shortcut Ctrl + Alt + L (++L), and gofmt will be run after GoLand’s formatter. The key word in this case is “whenever,” as users don’t have full control over the precise moment a file gets modified. We have introduced the Run gofmt on code reformat option. Whenever a Go file is changed, the command is executed on it. It’s easy to add a new watcher by using the predefined ‘go fmt’ template. Under Settings (Preferences), you can find Tools | File Watchers. The third way is to configure a file watcher. There you can find Go fmt, which is enabled by default. They can be configured via Settings (Preferences on macOS) | Version Control | Commit | Before Commit. There are several hooks that can be run before a commit. The sole purpose of these actions is to run ‘go fmt’, providing it with either file paths or a path to the project directory, and they only work when you invoke them manually. Each has a shortcut assigned to it: Alt + Shift + Control + F (Alt + Shift + Cmd + F on macOS) and Alt + Shift + Control + P (Alt + Shift + Cmd + P), respectively. Under the Tools | Go Tools menu, you can find two actions related to code formatting: Go Fmt File and Go Fmt Project. In summary the current state (from the link) is:Ĭurrently, GoLand offers three ways to interact with ‘go fmt’: dedicated actions, before commit tools, and file watchers. You can run go fmt on save but there are a number of factors to consider which are covered in this blog post (main consideration is that GoLand has its own formatter). imported and not used: "net/http") so this may be something you want to resolve before checking code in.Īlso, is it possible to set Goland so that it automatically formats (as in go fmt) on save? Note that unused imports result in a compilation error (e.g. I suspect that this (Git | Commit -> Settings -> Optimize imports ) is the option you are looking for (note that there are a lot of other relevant settings on the referenced page). Is it possible to tweak how Goland automatically formats, adds, and removes imports?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |