【发布时间】:2015-07-17 10:11:31
【问题描述】:
在 Golang 中从切片创建自己的类型是个好主意吗?
例子:
type Trip struct {
From string
To string
Length int
}
type Trips []Trip // <-- is this a good idea?
func (trips *Trips) TotalLength() int {
ret := 0
for _, i := range *trips {
ret += i.Length
}
return ret
}
在我的示例中创建像 Trips 这样的类型是 Golang 中的某种约定吗?还是在整个项目中使用[]Trip比较好?有什么优缺点吗?
【问题讨论】:
-
是的,如果您能理解这样的旅行。专家建议根据正在解决的问题来命名类型。
标签: go conventions convention