Loading...
Loading...
Compare original and translation side by side
structconstvarNewXxxnewXxxfunc (s *something) Cost() int {
return calcCost(s.weights)
}
type something struct{ ... }
func calcCost(n []int) int { ... }
func (s *something) Stop() { ... }
func newSomething() *something {
return &something{}
}type something struct{ ... }
func newSomething() *something {
return &something{}
}
func (s *something) Cost() int {
return calcCost(s.weights)
}
func (s *something) Stop() { ... }
func calcCost(n []int) int { ... }structconstvarNewXxxnewXxxfunc (s *something) Cost() int {
return calcCost(s.weights)
}
type something struct{ ... }
func calcCost(n []int) int { ... }
func (s *something) Stop() { ... }
func newSomething() *something {
return &something{}
}type something struct{ ... }
func newSomething() *something {
return &something{}
}
func (s *something) Cost() int {
return calcCost(s.weights)
}
func (s *something) Stop() { ... }
func calcCost(n []int) int { ... }func (r *SomeType) SomeLongFunctionName(foo1, foo2, foo3 string,
foo4, foo5, foo6 int) {
foo7 := bar(foo1)
}func (r *SomeType) SomeLongFunctionName(
foo1, foo2, foo3 string,
foo4, foo5, foo6 int,
) {
foo7 := bar(foo1)
}// Good: factor out locals
local := helper(some, parameters, here)
result := foo.Call(list, of, parameters, local)func (r *SomeType) SomeLongFunctionName(foo1, foo2, foo3 string,
foo4, foo5, foo6 int) {
foo7 := bar(foo1)
}func (r *SomeType) SomeLongFunctionName(
foo1, foo2, foo3 string,
foo4, foo5, foo6 int,
) {
foo7 := bar(foo1)
}// Good: factor out locals
local := helper(some, parameters, here)
result := foo.Call(list, of, parameters, local)// Bad
printInfo("foo", true, true)
// Good
printInfo("foo", true /* isLocal */, true /* done */)booltype Region int
const (
UnknownRegion Region = iota
Local
)
func printInfo(name string, region Region, status Status)// Bad
printInfo("foo", true, true)
// Good
printInfo("foo", true /* isLocal */, true /* done */)booltype Region int
const (
UnknownRegion Region = iota
Local
)
func printInfo(name string, region Region, status Status)// Bad: pointer to interface is almost always wrong
func process(r *io.Reader) { ... }
// Good: pass the interface value directly
func process(r io.Reader) { ... }// Bad: pointer to interface is almost always wrong
func process(r *io.Reader) { ... }
// Good: pass the interface value directly
func process(r io.Reader) { ... }%q%q%q// Good: %q makes boundaries and special chars visible
fmt.Printf("value %q looks like English text", someText)
// Bad: manually adding quotes
fmt.Printf("value \"%s\" looks like English text", someText)%q%q// Good: %q makes boundaries and special chars visible
fmt.Printf("value %q looks like English text", someText)
// Bad: manually adding quotes
fmt.Printf("value \"%s\" looks like English text", someText)%qPrintfconstgo vet// Bad: variable format string — go vet can't check it
msg := "unexpected values %v, %v\n"
fmt.Printf(msg, 1, 2)
// Good: const format string — go vet can validate
const msg = "unexpected values %v, %v\n"
fmt.Printf(msg, 1, 2)Printfconstgo vet// Bad: variable format string — go vet can't check it
msg := "unexpected values %v, %v\n"
fmt.Printf(msg, 1, 2)
// Good: const format string — go vet can validate
const msg = "unexpected values %v, %v\n"
fmt.Printf(msg, 1, 2)fgo vet// Good: go vet checks Wrapf format strings by default
func Wrapf(err error, format string, args ...any) error
// Bad: go vet won't check Wrap's format string
func Wrap(err error, format string, args ...any) errorgo vet -printfuncs=wrapf,statusfgo-functions/references/FUNCTIONAL-OPTIONS.mdfgo vet// Good: go vet checks Wrapf format strings by default
func Wrapf(err error, format string, args ...any) error
// Bad: go vet won't check Wrap's format string
func Wrap(err error, format string, args ...any) errorgo vet -printfuncs=wrapf,statusfgo-functions/references/FUNCTIONAL-OPTIONS.md| Topic | Rule |
|---|---|
| File ordering | Type -> constructor -> exported -> unexported -> utils |
| Signature wrapping | All args on own lines with trailing comma |
| Naked parameters | Add |
| Pointers to interfaces | Almost never needed; pass interfaces by value |
| Use for human-readable string output |
| Format string storage | Declare as |
| Printf function names | End with |
| 主题 | 规则 |
|---|---|
| 文件排序 | 类型 -> 构造函数 -> 导出函数 -> 未导出函数 -> 工具函数 |
| 签名换行 | 所有参数单独占一行并添加 trailing comma |
| 裸参数 | 添加 |
| 指向接口的指针 | 几乎不需要;按值传递接口 |
| 用于人类可读的字符串输出 |
| 格式化字符串存储 | 在Printf调用外部声明为 |
| Printf函数命名 | 以 |