【发布时间】:2016-10-06 16:02:47
【问题描述】:
https://play.golang.org/p/qxhocI6mjY
在这部剧中,我得到了这个错误:invalid operation: s[0] (type AlmostSlice does not support indexing)
所以我想知道,是否可以实现索引?
给定这样的结构:
type AlmostSlice struct {
Entities []string
Id string
Stuffs string
}
是否可以让它支持索引?
s := AlmostSlice{Id: "bar", Entities: []string{"foo"}}
... := s[0]
s[0] = "stuffs"
例如,通过实现这样的东西:
func (s *AlmostSlice) Index(i int) string {
return s.Entities[i]
}
【问题讨论】:
-
没有。来自spec:索引表达式表示“数组的元素、指向数组、切片、字符串或映射的指针”
标签: go