go-reflect

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:

v.Elem() Vs Indirect(v) when passing in result of a reflect.New(Type)

感情迁移 提交于 2020-01-02 11:59:10
问题 My question is related to this question here: golang - Elem Vs Indirect in the reflect package Basically it claims that the expression below is true if someX is a reflect.Value that contains a pointer reflect.Indirect(reflect.ValueOf(someX)) === reflect.ValueOf(someX).Elem() If that is the case, then why does my code below crash on the last line? package main import ( "reflect" "log" ) type Person struct { Name string } func main() { newitem := reflect.New(reflect.ValueOf(Person{}).Type())

Golang reflect: Get Type representation from name?

被刻印的时光 ゝ 提交于 2019-12-28 07:00:49
问题 Is there a way to use the reflection libraries in Go to go from the name of a type to its Type representation? I've got a library where the user needs to provide Type representations for some code generation. I know it must be possible (in a sense) because they can just create a variable of that type and call the TypeOf function, but is there a way to circumvent this and just get representation from the name? 回答1: The question is not quite explicit, it can be interpreted in 2 ways, to one of

How to reflect struct recursive in golang

我是研究僧i 提交于 2019-12-23 02:36:05
问题 I want to reflect the struct type and value recursively, but it fails. I don't know how to pass the sub struct recursively. It error the following. panic: reflect: NumField of non-struct type goroutine 1 [running]: reflect.(*rtype).NumField(0xc0b20, 0xc82000a360) /usr/local/go/src/reflect/type.go:660 +0x7b I have two struct Person and Name type Person struct { Fullname NameType Sex string } type Name struct { Firstname string Lastname string } I define the Person in main, and display the

Golang: get the type of slice

こ雲淡風輕ζ 提交于 2019-12-22 03:22:58
问题 I am using reflect package to get the type of arbitrary array, but getting prog.go:17: cannot use sample_array1 (type []int) as type []interface {} in function argument [process exited with non-zero status] How do I get the type from array? I know how to get it from value. func GetTypeArray(arr []interface{}) reflect.Type { return reflect.TypeOf(arr[0]) } http://play.golang.org/p/sNw8aL0a5f 回答1: Change: GetTypeArray(arr []interface{}) to: GetTypeArray(arr interface{}) By the way, []int is not

Why golang reflect.MakeSlice returns un-addressable Value

纵然是瞬间 提交于 2019-12-18 10:35:48
问题 check the Snippet below: http://play.golang.org/p/xusdITxgT- Why is this happening? Because one of my argument must be a slice address. Maybe I did not made it clear for everyone. collection.Find(bson.M{}).All(&result) The above code is why I need a slice address. the result variable here is what I need. Now usually I can do this result := make([]SomeStruct, 10, 10) But now the SomeStruct is dynamic and I need to create the slice by using reflect.MakeSlice, So result := reflect.MakeSlice

Identify non builtin-types using reflect

北城余情 提交于 2019-12-17 09:53:53
问题 I need to differentiate such types as type A []byte from a []byte . Using reflect , reflect.TypeOf(A{}).Kind tells me that it is a Slice of byte . How can I differentiate []byte{} from A{} , without having a bounded list of types to check for? Are there new ways to do it in newer versions of Go? 回答1: Some background First let's clear some things related to types. Quoting from Spec: Types: A type determines the set of values and operations specific to values of that type. Types may be named or

How do I compare two functions for pointer equality in the latest Go weekly?

不问归期 提交于 2019-12-17 09:23:23
问题 In Go, is there any way to compare two non-nil function pointers to test for equality? My standard of equality is pointer equality. If not, is there any particular reason why pointer equality is not allowed? As of now, if I attempt to do this in the straight-forward way: package main import "fmt" func SomeFun() { } func main() { fmt.Println(SomeFun == SomeFun) } I get ./func-pointers.go:12: invalid operation: SomeFun == SomeFun (func can only be compared to nil) It is my understanding that

How do I compare two functions for pointer equality in the latest Go weekly?

南笙酒味 提交于 2019-12-17 09:21:20
问题 In Go, is there any way to compare two non-nil function pointers to test for equality? My standard of equality is pointer equality. If not, is there any particular reason why pointer equality is not allowed? As of now, if I attempt to do this in the straight-forward way: package main import "fmt" func SomeFun() { } func main() { fmt.Println(SomeFun == SomeFun) } I get ./func-pointers.go:12: invalid operation: SomeFun == SomeFun (func can only be compared to nil) It is my understanding that

Access struct property by name

别等时光非礼了梦想. 提交于 2019-12-17 07:12:52
问题 Here is a simple go program that is not working : package main import "fmt" type Vertex struct { X int Y int } func main() { v := Vertex{1, 2} fmt.Println(getProperty(&v, "X")) } func getProperty(v *Vertex, property string) (string) { return v[property] } Error: prog.go:18: invalid operation: v[property] (index of type *Vertex) What I want is to access the Vertex X property using its name. If I do v.X it works, but v["X"] doesn't. Can someone tell me how to make this work ? 回答1: Most code