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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动