避免参数语义不明确
函数调用中的意义不明确的参数可能会损害可读性。当参数名称的含义不明显时,请为参数添加C-style的注释 (/* ... */)。
以func printInfo(name string, isLocal, done bool)为例:
// bad usage
printInfo("foo", true, true)
// good usage
printInfo("foo", true /* isLocal */, true /* done */)对于上面的示例代码,还有一种更好的处理方式是将上面的 bool 类型换成自定义类型。将来,该参数可以支持不仅仅局限于两个状态(true/false)。
type Region int
const (
  UnknownRegion Region = iota + 1
  Local
)
type Status int
const (
  StatusReady = iota + 1
  StatusDone
  // Maybe we will have a StatusInProgress in the future.
)
func printInfo(name string, region Region, status Status)Last updated
Was this helpful?