go-reflect

How to find a type of an object in Go?

て烟熏妆下的殇ゞ 提交于 2019-11-28 02:33:06
How do I find the type of an object in Go? In Python, I just use typeof to fetch the type of object. Similarly in Go, is there a way to implement the same ? Here is the container from which I am iterating: for e := dlist.Front(); e != nil; e = e.Next() { lines := e.Value fmt.Printf(reflect.TypeOf(lines)) } I am not able to get the type of the object lines in this case which is an array of strings. The Go reflection package has methods for inspecting the type of variables. The following snippet will print out the reflection type of a string, integer and float. package main import ( "fmt"

range over interface{} which stores a slice

被刻印的时光 ゝ 提交于 2019-11-27 10:59:37
Given the scenario where you have a function which accepts t interface{} . If it is determined that the t is a slice, how do I range over that slice? I will not know the incoming type, such as []string , []int or []MyType , at compile time. func main() { data := []string{"one","two","three"} test(data) moredata := []int{1,2,3} test(data) } func test(t interface{}) { switch reflect.TypeOf(t).Kind() { case reflect.Slice: // how do I iterate here? for _,value := range t { fmt.Println(value) } } } Go Playground Example: http://play.golang.org/p/DNldAlNShB Well I used reflect.ValueOf and then if it

How do you create a new instance of a struct from its type at run time in Go?

我是研究僧i 提交于 2019-11-27 10:39:19
In Go, how do you create the instance of an object from its type at run time? I suppose you would also need to get the actual type of the object first too? I am trying to do lazy instantiation to save memory. In order to do that you need reflect . package main import ( "fmt" "reflect" ) func main() { // one way is to have a value of the type you want already a := 1 // reflect.New works kind of like the built-in function new // We'll get a reflected pointer to a new int value intPtr := reflect.New(reflect.TypeOf(a)) // Just to prove it b := intPtr.Elem().Interface().(int) // Prints 0 fmt

Call a Struct and its Method by name in Go?

谁都会走 提交于 2019-11-27 10:28:19
I have found a function call MethodByName() here http://golang.org/pkg/reflect/#Value.MethodByName but it's not exactly what I want! (maybe because I don't know how to use it ... I cannot find any example with it). What I want is: type MyStruct struct { //some feilds here } func (p *MyStruct) MyMethod { println("My statement."); } CallFunc("MyStruct", "MyMethod"); //print out My statement." So I guess, first I need something like StructByName() and after that use it for MethodByName() , is that right!? To call a method on an object, first use reflect.ValueOf . Then find the method by name, and

How to get the name of a function in Go?

回眸只為那壹抹淺笑 提交于 2019-11-27 09:10:59
问题 Given a function, is it possible to get its name? Say: func foo() { } func GetFunctionName(i interface{}) string { // ... } func main() { // Will print "name: foo" fmt.Println("name:", GetFunctionName(foo)) } I was told that runtime.FuncForPC would help, but I failed to understand how to use it. 回答1: Sorry for answering my own question, but I found a solution: package main import ( "fmt" "reflect" "runtime" ) func foo() { } func GetFunctionName(i interface{}) string { return runtime.FuncForPC

How to find a type of an object in Go?

与世无争的帅哥 提交于 2019-11-27 08:56:01
问题 How do I find the type of an object in Go? In Python, I just use typeof to fetch the type of object. Similarly in Go, is there a way to implement the same ? Here is the container from which I am iterating: for e := dlist.Front(); e != nil; e = e.Next() { lines := e.Value fmt.Printf(reflect.TypeOf(lines)) } I am not able to get the type of the object lines in this case which is an array of strings. 回答1: The Go reflection package has methods for inspecting the type of variables. The following

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

别等时光非礼了梦想. 提交于 2019-11-27 07:36: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 this behavior was introduced recently. I've found an answer using the reflect package; however Atom

Using reflect, how do you set the value of a struct field?

我们两清 提交于 2019-11-27 05:59:15
having a rough time working with struct fields using reflect package. in particular, have not figured out how to set the field value. type t struct { fi int; fs string } var r t = t{ 123, "jblow" } var i64 int64 = 456 getting Name of field i - this seems to work var field = reflect.TypeOf(r).Field(i).Name getting value of field i as a) interface{}, b) int - this seems to work var iface interface{} = reflect.ValueOf(r).Field(i).Interface() var i int = int(reflect.ValueOf(r).Field(i).Int()) setting value of field i - try one - panic reflect.ValueOf(r).Field(i).SetInt( i64 ) panic : reflect.Value

Access struct property by name

旧时模样 提交于 2019-11-27 03:22:44
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 ? Most code shouldn't need this sort of dynamic lookup. It's inefficient compared to direct access (the compiler knows the

Iterate through the fields of a struct in Go

半世苍凉 提交于 2019-11-27 03:02:59
Basically, the only way (that I know of) to iterate through the values of the fields of a struct is like this: type Example struct { a_number uint32 a_string string } //... r := &Example{(2 << 31) - 1, "...."}: for _, d:= range []interface{}{ r.a_number, r.a_string, } { //do something with the d } I was wondering, if there's a better and more versatile way of achieving []interface{}{ r.a_number, r.a_string, } , so I don't need to list each parameter individually, or alternatively, is there a better way to loop through a struct? I tried to look through the reflect package, but I hit a wall,