Using reflect to update value by reference when argument is not a pointer in go

回眸只為那壹抹淺笑 提交于 2020-01-17 01:37:08

问题


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:


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.



来源:https://stackoverflow.com/questions/52125578/using-reflect-to-update-value-by-reference-when-argument-is-not-a-pointer-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!