在Go语言中修改结构体中的结构体切片。

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

Modifying a struct slice within a struct in Go

问题

在下面的示例中,一个 person 拥有一组 friendship 的切片,我试图将一个 friendship 初始化为指向另一个 person 对象的指针,但由于某种原因失败了,结果是没有人有任何 friendship。我是否在某个地方没有正确使用指针?

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. )
  6. type friendship struct {
  7. friend *person
  8. }
  9. type person struct {
  10. name int
  11. friendship []friendship
  12. }
  13. func createPerson(id int) person {
  14. return person{id, make([]friendship, 0)}
  15. }
  16. func (p *person) addFriends(possibleFriends []*person, numFriends int) {
  17. var friend *person
  18. for i := 0; i < numFriends; i++ {
  19. friend = possibleFriends[rand.Intn(len(possibleFriends))]
  20. p.friendship = append(p.friendship, friendship{friend})
  21. }
  22. }
  23. func main() {
  24. numPeople := 20
  25. people := make([]person, numPeople)
  26. possibleFriends := make([]*person, numPeople)
  27. for i := 0; i < numPeople; i++ {
  28. people[i] = createPerson(i)
  29. possibleFriends[i] = &(people[i])
  30. }
  31. for _, p := range people {
  32. p.addFriends(possibleFriends, 2)
  33. }
  34. fmt.Println(people)
  35. }

以下是翻译好的代码部分,不包括问题部分。

英文:

In the following example, a person has a slice of friendships, and I try to initialize a friendship as a pointer to another person object, but for some reason it fails, and the result is that nobody has any friendships. Am I not using a pointer somewhere where I should be?

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;math/rand&quot;
  5. )
  6. type friendship struct {
  7. friend *person
  8. }
  9. type person struct {
  10. name int
  11. friendship []friendship
  12. }
  13. func createPerson(id int) person {
  14. return person{id, make([]friendship, 0)}
  15. }
  16. func (p *person) addFriends(possibleFriends []*person, numFriends int) {
  17. var friend *person
  18. for i := 0; i &lt; numFriends; i++ {
  19. friend = possibleFriends[rand.Intn(len(possibleFriends))]
  20. p.friendship = append(p.friendship, friendship{friend})
  21. }
  22. }
  23. func main() {
  24. numPeople := 20
  25. people := make([]person, numPeople)
  26. possibleFriends := make([]*person, numPeople)
  27. for i := 0; i &lt; numPeople; i++ {
  28. people[i] = createPerson(i)
  29. possibleFriends[i] = &amp;(people[i])
  30. }
  31. for _, p := range people {
  32. p.addFriends(possibleFriends, 2)
  33. }
  34. fmt.Println(people)
  35. }

答案1

得分: 2

使用以下代码替换原始代码中的部分内容:

  1. for i := 0; i < numPeople; i++ {
  2. people[i].addFriends(possibleFriends, 2)
  3. }

或者

  1. for i, _ := range people {
  2. people[i].addFriends(possibleFriends, 2)
  3. }

替代原始代码中的部分:

  1. for _, p := range people {
  2. p.addFriends(possibleFriends, 2)
  3. }

这是因为ppeople[i]的副本,对切片peopleaddFriends操作没有影响。

英文:

use

  1. for i := 0; i &lt; numPeople; i++ {
  2. people[i].addFriends(possibleFriends, 2)
  3. }

or

  1. for i, _ := range people {
  2. people[i].addFriends(possibleFriends, 2)
  3. }

instead of

  1. for _, p := range people {
  2. p.addFriends(possibleFriends, 2)
  3. }

this is because p is a copy of people[i], addFriends has no effect on slice people

huangapple
  • 本文由 发表于 2017年3月7日 11:42:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/42639776.html
匿名

发表评论

匿名网友

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

确定