Table of Contents
跟Vim, VSCode一样,GoLand也支持在保存代码时,自动调用 gofmt 和 goimports 来自动格式化代码。但是在网上搜到的都是之前版本的GoLand配置,在最新版的GoLand中,是通过File Watchers插件来实现此功能的。
1. File Watchers 插件安装
安装”File Watchers”的插件,并设置为启用状态
在Tools -> File Watchers中,添加如下设置:
1.1 File Watchers配置参数说明:
Name : golint #Tool名称 Description: golint #Tool描述 Program : $GOPATH\bin\golint.exe #选择golint可执行文件的位置 Atguments: $FilePath$ #魔法变量(执行当前go文件的路径) Working directory: $ProjectFileDir$ #魔法变量(当前项目的路径)
2. gofmt插件配置
3. goimports插件配置
4. golint配置到File Watchers
在Tools -> File Watchers -> custom中,添加如下设置:
3.1 示例
修改前文件
// ObtainPD Post Data
type ObtainPD struct {
CouponTemplateID int64 `json:"coupon_template_id"`
AppKey string `json:"app_key"`
UID int64 `json:"uid"`
}
修改后文件
// Obtain PD Post Data
type ObtainPD struct {
CouponTemplateID int64 `json:"coupon_template_id"`
AppKey string `json:"app_key"`
UID int64 `json:"uid"`
}
保存时文件时自动执行golint
$GOPATH/bin/golint PathTo/controllers/coupon/obtain.goPathTo/controllers/coupon/obtain.go:13:1: comment on exported type ObtainPD should be of the form "ObtainPD ..." (with optional leading article)
Process finished with exit code 0