为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
慕村9548890 2021-08-10 15:06:21
我正在尝试遍历切片值并更新一个或多个值,通过索引访问它或遍历它们。然后我有以下问题。package mainimport "fmt"type Member struct {    Name, Surname string    Age           int}type Members []Memberfunc 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)
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 301 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信