从 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
}
在Golang中像我例子中创建类型Trips
是一种惯例吗?还是在整个项目中使用[]Trip
更好?有什么利弊?