dongyanju0945 2018-10-15 19:24
浏览 26
已采纳

追加在不存在的其他切片内部的结构上切片

For example:

package main

import "fmt"

type Test struct {
  elems []string
}

func main() {
  initial := Test{
    elems: make([]string, 0),
  }
  initial.elems = append(initial.elems, "apple")
  fmt.Println(initial.elems) // #1 [apple]

  s := make([]Test, 0)
  s = append(s, initial)

  initial.elems = append(initial.elems, "bannana")
  fmt.Println(initial.elems) // #2 [apple bannana]
  fmt.Println(s[0].elems) // #3 [apple]

  second := s[0]
  second.elems = append(second.elems, "carrot")
  fmt.Println(second.elems) // #4 [apple bannana]
}

I am looking for help understanding print statements #3 and #4. In #3 I expect [apple bannana] and in #4 I am expecing [apple bannana carrot].

It is my understanding that the elems field which is a slice is automatically passed by reference and therefore each append that I do in the above block of code should modify the underlying array. But, apparently that is not the case.

So, my question is: What happens to initial when it gets inserted into a slice that makes this not work? Also, how would one write this code to get the expected result at print statement #4?

  • 写回答

2条回答 默认 最新

  • duanpo7282 2018-10-15 19:56
    关注

    In Golang it is mentioned:

    Map and slice values behave like pointers: they are descriptors that contain pointers to the underlying map or slice data. Copying a map or slice value doesn't copy the data it points to.

    The way you are appending to the slice s is creating a new slice by adding the copy of Test struct to the s slice. Hence you are not setting a pointer to the original Test struct. So that if data changes inside the struct it will be reflected in the slice too. This is the problem you are facing.

      initial.elems = append(initial.elems, "apple")
      fmt.Println(initial.elems) // #1 [apple]
    
      s := make([]Test, 0) // this should be pointer to the struct to have teh changes in future to original struct.
      s = append(s, initial) // appending to the s slice
    

    Create a pointer to the Test struct when making slice s, which will reflect the change whenever you change the elems inside original struct. For example:

    package main
    import "fmt"
    
    type Test struct {
      elems []string
    }
    
    func main() {
      initial := Test{
        elems: make([]string, 0),
      }
      initial.elems = append(initial.elems, "apple")
      fmt.Println(initial.elems) // #1 [apple]
    
      s := make([]*Test, 0) // create a pointer to Test struct.
      s = append(s, &initial)
    
      initial.elems = append(initial.elems, "bannana")
      fmt.Println(initial.elems) // #2 [apple bannana]
      fmt.Printf("%+v
    ",*s[0]) // #3 [apple banana]
    
      second := s[0]
      second.elems = append(second.elems, "carrot")
      fmt.Println(second.elems) // #4 [apple bannana carrot]
    }
    

    Output:-

    [apple]
    [apple bannana]
    {elems:[apple bannana]}
    [apple bannana carrot]
    

    Working Code on Go Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢