简体   繁体   English

goland如何使用gofmt?

[英]goland how to use gofmt?

I started learning golang and installed GoLand from JetBrains .我开始学习 golang 并从 JetBrains安装了GoLand I am reading the book The Go Programming Language , and in the first chapter author strongly recommends using the gofmt tool before each save.我正在阅读The Go Programming Language一书,在第一章中,作者强烈建议在每次保存之前使用gofmt工具。 How I can follow this recommendation in GoLand?我如何在 GoLand 中遵循此建议?

There are ways to format your code , you can do that by using any one of the way :有多种方法可以格式化您的代码,您可以使用以下任何一种方式来做到这一点:

  1. once your code is done.一旦你的代码完成。 just run the command " gofmt -s -w ."只需运行命令“ gofmt -s -w ”。 in terminal in required directory or else in needed file.在所需目录中的终端或所需文件中。 it will format your whole directory/file as per your need .它将根据您的需要格式化您的整个目录/文件。
  2. go to preferences ->Tools ->File Watchers and enable go fmt .转到首选项 -> 工具 -> 文件观察器并启用 go fmt 。 This way on each save it will format the file.这样在每次保存时都会格式化文件。

GoLand have commands to go fmt your file or project. GoLand 有命令来go fmt您的文件或项目。 Right click on your file and you will find it under "Go tools".右键单击您的文件,您将在“Go tools”下找到它。 You could see its shortcut there.你可以在那里看到它的快捷方式。

You can also use "Reformat code" command (bound to Command-Alt-L for me).您还可以使用“重新格式化代码”命令(对我来说绑定到 Command-Alt-L)。 It's not 100% same as go fmt but very close.它与go fmt不是 100% 相同,但非常接近。 Also works for other languages.也适用于其他语言。

If you have an open terminal, you can run go fmt ./... to format all files in a directory (including sub directories).如果你有一个打开的终端,你可以运行go fmt ./...来格式化目录(包括子目录)中的所有文件。 You can put this in a git commit hook to run it every time you commit.你可以把它放在一个 git commit 钩子中,以便在你每次提交时运行它。

As others mentioned there's a file watcher section under Preferences → Tools → File Watchers, in there you can setup to run go fmt or goimports everytime you save a Go file.正如其他人提到的,在首选项 → 工具 → 文件观察器下有一个文件观察器部分,在那里你可以设置每次保存 Go 文件时运行go fmtgoimports

Just want to add 1 more option.只想再添加 1 个选项。 You can automatically apply go fmt every time when you are about to commit to git.每次准备提交到 git 时,都可以自动应用 go fmt。 When you open the commit window, under the Before Commit section, there is a Go fmt checkbox.当您打开提交窗口时, Before Commit部分下,有一个Go fmt复选框。 Just check it.只需检查一下。 Later when you do another commit, the default value for that checkbox is checked.稍后当您再次提交时,该复选框的默认值会被选中。 So, you only need to check it once.所以,你只需要检查一次。 Then go fmt will run automatically before you commit.然后 go fmt 将在您提交之前自动运行。

If your version of GoLand 2021.2 or higher, you can enable Run gofmt on code reformat option under Preferences/Settings |如果您的 GoLand 2021.2 或更高版本,您可以在Preferences/Settings |下启用Run gofmt on code reformat选项 Editor |编辑| Code Style |代码风格 | Go |去 | Other (it is enabled by default).其他(默认启用)。

在此处输入图片说明

After that, execute Code |之后,执行Code | Reformat Code ( Option + Command + L on macOS) and gofmt will be executed after internal GoLand formatter.重新格式化代码(macOS 上的Option + Command + L )和 gofmt 将在内部 GoLand 格式化程序之后执行。

Now Goland has a section that will reformat your code on file save without setting up File Watchers :现在 Goland 有一个部分可以在不设置File Watchers 的情况下重新格式化您的文件保存代码:

  • Under Settings -> Tools -> Actions On Save you can activate the first entrySettings -> Tools -> Actions On Save您可以激活第一个条目

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM