dow46218 2018-09-01 06:39
浏览 61
已采纳

当参数不是go中的指针时,使用反射通过引用更新值

I've had difficulty learning the basics of reflect, pointers and interface in go, so here's another entry level question I can't seem to figure out.

This code does what I want it to do - I'm using reflect to add another record to a slice that's typed as an interface.

package main

import (
  "reflect"
  "log"
)
type Person struct {
  Name string
}
func Add(slice interface{}) {
  s := reflect.ValueOf(slice).Elem()
  // in my actual code, p is declared via the use of reflect.New([Type])
  p := Person{Name:"Sam"}

  s.Set(reflect.Append(s,reflect.ValueOf(p)))
}

func main() {
  p := []Person{}
  Add(&p)
  log.Println(p)
}

If I changed the Add and main function to this, things don't work the way I want it to.

func Add(slice interface{}) {
  s := reflect.ValueOf(&slice).Elem()
  p := Person{Name:"Sam"}

  s.Set(reflect.Append(reflect.ValueOf(slice),reflect.ValueOf(p)))
  log.Println(s)
}

func main() {
  p := []Person{}
  Add(p)
  log.Println(p)
}

That is, the log.Println(p) at the end doesn't show a slice with the record Sam in it like the way I had hoped. So my question is whether it's possible for me to have Add() receive a slice that is not a pointer, and for me to still write some code in Add() that will produce the outcome shown in my first scenario?

A lot of my recent questions dance around this kind of subject, so it's still taking me a while to figure out how to use the reflect package effectively.

  • 写回答

1条回答 默认 最新

  • doushang4274 2018-09-01 11:07
    关注

    No, it's not possible to append to a slice in a function without passing in a pointer to the slice. This isn't related to reflection, but to how variables are passed in to functions. Here's the same code, modified to not use reflection:

    package main
    
    import (
            "log"
    )
    
    type Person struct {
            Name string
    }
    
    func AddWithPtr(slicep interface{}) {
            sp := slicep.(*[]Person)
    
            // This modifies p1 itself, since *sp IS p1
            *sp = append(*sp, Person{"Sam"})
    }
    
    func Add(slice interface{}) {
            // s is now a copy of p2
            s := slice.([]Person)
    
            sp := &s
    
            // This modifies a copy of p2 (i.e. s), not p2 itself
            *sp = append(*sp, Person{"Sam"})
    }
    
    func main() {
            p1 := []Person{}
            // This passes a reference to p1
            AddWithPtr(&p1)
            log.Println("Add with pointer:   ", p1)
    
            p2 := []Person{}
            // This passes a copy of p2
            Add(p2)
            log.Println("Add without pointer:", p2)
    }
    

    (Above, when it says 'copy' of the slice, it doesn't mean the copy of the underlying data - just the slice)

    When you pass in a slice, the function effectively gets a new slice that refers to the same data as the original. Appending to the slice in the function increases the length of the new slice, but doesn't change the length of the original slice that was passed in. That's why the original slice remains unchanged.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计