英文:
Go update slice iterating error "does not support indexing"
问题
我正在尝试通过索引或迭代来遍历切片值并更新一个或多个值。然后我遇到了以下问题。
package main
import "fmt"
type Member struct {
Name, Surname string
Age int
}
type Members []Member
func main() {
members1 := Members{
{"Bob", "Doe", 30},
{"Jack", "Doe", 30},
{"John", "Doe", 30},
}
members1.ModifyName()
members1.ModifyAllNames1()
members1.ModifyAllNames2()
fmt.Println(members1)
}
func (members *Members) ModifyName() {
(*members)[0].Name = "-"
}
func (members *Members) ModifyAllNames1() {
for i := 0; i < len(*members); i++ {
(*members)[i].Name = "-"
}
}
func (members *Members) ModifyAllNames2() {
for _, m := range *members {
m.Name = "-"
}
}
然后我得到了以下错误:
invalid operation: members[0] (type *Members does not support indexing)
invalid operation: members[i] (type *Members does not support indexing)
请注意,你需要使用(*members)
来访问Members
类型的切片元素,因为members
是一个指向Members
的指针。
英文:
I'm trying to iterate through slice values and update one or more values, accessing it by index or iterating through them. Then I have the following problem.
package main
import "fmt"
type Member struct {
Name, Surname string
Age int
}
type Members []Member
func main() {
members1 := Members{
{"Bob", "Doe", 30},
{"Jack", "Doe", 30},
{"John", "Doe", 30},
}
members1.ModifyName()
members1.ModifyAllNames1()
members1.ModifyAllNames2()
fmt.Println(members1)
}
func (members *Members) ModifyName() {
*members[0].Name = "-"
}
func (members *Members) ModifyAllNames1() {
for i := 0; i < len(*members); i++ {
*members[i].Name = "-"
}
}
func (members *Members) ModifyAllNames2() {
for _, m := range *members {
m.Name = "-"
}
}
Then I get the following errors:
invalid operation: members[0] (type *Members does not support indexing)
invalid operation: members[i] (type *Members does not support indexing)
答案1
得分: 84
类型*Members
不支持索引,类型Members
支持。
m := make([]Members, 5)
pm := &m
m[:2] // 正确
pm[:2] // 错误
(*pm)[:2] // 正确
https://go.dev/play/p/CDaBKqB-sqM
package main
import "fmt"
type Member struct {
Name, Surname string
Age int
}
type Members []Member
func main() {
members1 := Members{
{"Bob", "Doe", 30},
{"Jack", "Doe", 30},
{"John", "Doe", 30},
}
members1.ModifyName()
members1.ModifyAllNames1()
//members1.ModifyAllNames2()
fmt.Println(members1)
}
func (members *Members) ModifyName() {
(*members)[0].Name = "-"
}
func (members *Members) ModifyAllNames1() {
for i := 0; i < len(*members); i++ {
(*members)[i].Name = "-"
}
}
func (members Members) ModifyAllNames2() {
for i := range members {
members[i].Name = "-"
}
}
英文:
Type *Members
does not support indexing, type Members
does.
m := make([]Members, 5)
pm := &m
m[:2] // good
pm[:2] // bad
(*pm)[:2] // good
https://go.dev/play/p/CDaBKqB-sqM
package main
import "fmt"
type Member struct {
Name, Surname string
Age int
}
type Members []Member
func main() {
members1 := Members{
{"Bob", "Doe", 30},
{"Jack", "Doe", 30},
{"John", "Doe", 30},
}
members1.ModifyName()
members1.ModifyAllNames1()
//members1.ModifyAllNames2()
fmt.Println(members1)
}
func (members *Members) ModifyName() {
(*members)[0].Name = "-"
}
func (members *Members) ModifyAllNames1() {
for i := 0; i < len(*members); i++ {
(*members)[i].Name = "-"
}
}
func (members Members) ModifyAllNames2() {
for i := range members {
members[i].Name = "-"
}
}
答案2
得分: 6
问题出在运算符的结合性和/或优先级上,尽管我在 Golang 规范中没有找到这个特定情况的明确说明。
表达式 *members[0].Name
等同于 *(members[0].Name)
,所以为了正确地进行指针解引用,你需要在需要解引用的项周围显式地加上一对括号,即 (*members)[0].Name
。
英文:
> func (members *Members) ModifyName() {
> *members[0].Name = "-"
> }
>
> invalid operation: members[0] (type *Members does not support indexing)
The issue is that of operator associativity and/or precedence, although I couldn't find this specific case explicitly covered within the Golang spec.
The expression *members[0].Name
appears to be equivalent to *(members[0].Name)
, so, to get the proper pointer deference, you gotta use a pair of parentheses explicitly around the item that must be dereferenced, as is, (*members)[0].Name
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论