避免参数语义不明确(Avoid Naked Parameters)
发布者:admin 发表于:417天前 阅读数:546 评论:0

函数调用中的意义不明确的参数可能会损害可读性。当参数名称的含义不明显时,请为参数添加 C 样式注释 (/ ... /)

Bad Good

// func printInfo(name string, isLocal, done bool)

printInfo("foo", true, true) |

// func printInfo(name string, isLocal, done bool)

printInfo("foo", true / isLocal /, true / done /) |

对于上面的示例代码,还有一种更好的处理方式是将上面的 bool 类型换成自定义类型。将来,该参数可以支持不仅仅局限于两个状态(true/false)。

type Region int

const (
  UnknownRegion Region = iota
  Local
)

type Status int

const (
  StatusReady Status= iota + 1
  StatusDone
  // Maybe we will have a StatusInProgress in the future.
)

func printInfo(name string, region Region, status Status)