函数调用中的意义不明确的参数(
Naked parameters )
可能会影响可读性,当参数名称的含义不明显时,请为参数添加 C 样式注释 (/* ... */
)
Bad
// func printInfo(name string, isLocal, done bool)
printInfo("foo", true, true)
Good
// func printInfo(name string, isLocal, done bool)
printInfo("foo", true /* isLocal */, true /* done */)
更好的是,用自定义类型替换原生的bool类型,这样代码可读性更好,类型安全。这允许该参数在将来有不止两种状态(true/false),具有可扩展性。
//自定义 int 类型
type Region int
//赋予类型不同的含义
const (
UnknownRegion Region = iota
Local
)
type Status int
const (
StatusReady Status = iota + 1
StatusDone
//将来可能会有 StatusInProgress .
)
func printInfo(name string, region Region, status Status)