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 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题