更新切片迭代错误“不支持索引”。

huangapple go评论80阅读模式
英文:

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 &quot;fmt&quot;

type Member struct {
	Name, Surname string
	Age           int
}
type Members []Member

func main() {
	members1 := Members{
		{&quot;Bob&quot;, &quot;Doe&quot;, 30},
		{&quot;Jack&quot;, &quot;Doe&quot;, 30},
		{&quot;John&quot;, &quot;Doe&quot;, 30},
	}
	members1.ModifyName()
	members1.ModifyAllNames1()
	members1.ModifyAllNames2()
	fmt.Println(members1)
}

func (members *Members) ModifyName() {

	*members[0].Name = &quot;-&quot;
}

func (members *Members) ModifyAllNames1() {

	for i := 0; i &lt; len(*members); i++ {
		*members[i].Name = &quot;-&quot;
	}
}

func (members *Members) ModifyAllNames2() {

	for _, m := range *members {
		m.Name = &quot;-&quot;
	}
}   

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 := &amp;m

m[:2]     // good
pm[:2]    // bad
(*pm)[:2] // good

https://go.dev/play/p/CDaBKqB-sqM

package main

import &quot;fmt&quot;

type Member struct {
	Name, Surname string
	Age           int
}
type Members []Member

func main() {
	members1 := Members{
		{&quot;Bob&quot;, &quot;Doe&quot;, 30},
		{&quot;Jack&quot;, &quot;Doe&quot;, 30},
		{&quot;John&quot;, &quot;Doe&quot;, 30},
	}
	members1.ModifyName()
	members1.ModifyAllNames1()
	//members1.ModifyAllNames2()
	fmt.Println(members1)
}

func (members *Members) ModifyName() {
	(*members)[0].Name = &quot;-&quot;
}

func (members *Members) ModifyAllNames1() {
	for i := 0; i &lt; len(*members); i++ {
		(*members)[i].Name = &quot;-&quot;
	}
}

func (members Members) ModifyAllNames2() {
	for i := range members {
		members[i].Name = &quot;-&quot;
	}
}

答案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.

huangapple
  • 本文由 发表于 2014年8月14日 00:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/25290956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定