代码必须用 gofmt 格式化。
一行代码不要超过120列,超过的情况,使用合理的换行方法换行。
例外场景:
采用惰性换行,换行前应尽可能的占满不留空位。
// Bad
fmt.Printf("%v %v %v %v %v %v %v %v %v %v %v %v %v %v\n",
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,89, 144, 233)
// Good
fmt.Printf("%v %v %v %v %v %v %v %v %v %v %v %v %v %v\n", 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
89, 144, 233)
// Bad
func foo() {
// func body
}
// Good
func foo() {
// func body
}
// Bad
res, err := foo()
if err != nil || res.Ret != 0 {
return
}
// Good
res, err := foo()
if err != nil || res.Ret != 0 {
return
}
// Bad
func foo() {
// func body
return
}
// Good
func foo() {
// func body
return
}
var f float64 = 0.2 + 0.8 // 运算符和操作数之间要留空格
fmt.Printf("%f\n", f+1) // a+1 作为入参不要留空格
// Bad
if foo && (int(bar) > 0) {
// ...
}
// Good
if foo && int(bar) > 0 {
// ...
}
使用反引号表示原始字符串字面值,避免转义
// Bad
wantError := "unknown name:\"test\""
// Good
wantError := `unknown error:"test"`
github.com/xxx/xxx
import (
// standard package
"encoding/json"
"strings"
// third-party package
"git.obc.im/obc/utils"
"git.obc.im/dep/beego"
"git.obc.im/dep/mysql"
elastic "github.com/olivere/elastic/v7"
// anonymous package
// import filesystem storage driver
_ "git.code.oa.com/org/repo/pkg/storage/filesystem
// inner package
"myproject/models"
"myproject/controller"
)
// 不要采用这种方式
import (
"../net"
)
// 应该采用完整路径引入包
import (
"xxxx.com/proj/net"
)
import (
elastic "github.com/olivere/elastic/v7"
)