Questions tagged [go-reflect]

Questions about the Go "reflect" package that implements run-time reflection, allowing a program to manipulate objects with arbitrary types.

Questions Using Go programming language reflect package

Package reflect implements run-time reflection, allowing a program to manipulate objects with arbitrary types. The typical use is to take a value with static type interface{} and extract its dynamic type information by calling TypeOf, which returns a Type.

A call to ValueOf returns a Value representing the run-time data. Zero takes a Type and returns a Value representing a zero value for that type.

See "The Laws of Reflection" for an introduction to reflection in Go: https://golang.org/doc/articles/laws_of_reflection.html

100 questions
557
votes
16 answers

How to find the type of an object in Go?

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 =…
Rahul
  • 11,129
  • 17
  • 63
  • 76
307
votes
10 answers

Type converting slices of interfaces

I'm curious why Go does't implicitly convert []T to []interface{} when it will implicitly convert T to interface{}. Is there something non-trivial about this conversion that I'm missing? Example: func foo([]interface{}) { /* do something */…
danny
  • 10,103
  • 10
  • 50
  • 57
208
votes
7 answers

How to compare if two structs, slices or maps are equal?

I want to check if two structs, slices and maps are equal. But I'm running into problems with the following code. See my comments at the relevant lines. package main import ( "fmt" "reflect" ) type T struct { X int Y string Z…
leiyonglin
  • 6,474
  • 12
  • 36
  • 41
188
votes
8 answers

Iterate through the fields of a struct in Go

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…
omninonsense
  • 6,644
  • 9
  • 45
  • 66
140
votes
3 answers

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

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 -…
cc young
  • 18,939
  • 31
  • 90
  • 148
139
votes
4 answers

How to get the name of a function in Go?

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…
moraes
  • 13,213
  • 7
  • 45
  • 59
139
votes
4 answers

range over interface{} which stores a slice

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? func main() { data := []string{"one","two","three"} test(data) moredata := []int{1,2,3} …
Owen Allen
  • 11,348
  • 9
  • 51
  • 63
108
votes
5 answers

Access struct property by name

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)…
Nicolas BADIA
  • 5,612
  • 7
  • 43
  • 46
100
votes
5 answers

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

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.
Mat Ryer
  • 3,797
  • 4
  • 26
  • 24
41
votes
5 answers

Call a Struct and its Method by name in Go?

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…
nvcnvn
  • 4,991
  • 8
  • 49
  • 77
39
votes
3 answers

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

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…
BurntSushi5
  • 13,917
  • 7
  • 52
  • 45
34
votes
1 answer

In golang, is it possible to get reflect.Type from the type itself, from name as string?

type t1 struct { i int; s string } var v1 reflect.Type = /* how to set to t1's reflect.Type? */ is it possible to get the reflect.Type of t1 without having to instantiate it? is it possible to get the reflect.Type of t1 from having its name "t1"…
cc young
  • 18,939
  • 31
  • 90
  • 148
29
votes
3 answers

How to check if an object has a particular method?

In Go, how do you check if an object responds to a method? For example, in Objective-C this can be achieved by doing: if ([obj respondsToSelector:@selector(methodName:)]) { // if method exists [obj methodName:42]; // call the method }
nishanthshanmugham
  • 2,967
  • 1
  • 25
  • 29
28
votes
4 answers

Get name of function using reflection

I'm trying to use Go's reflection system to retrieve the name of a function but I get an empty string when calling the Name method on its type. Is this the expected behavior? This is a simple example of how I approach the problem: package…
Laserallan
  • 11,072
  • 10
  • 46
  • 67
25
votes
2 answers

Why golang reflect.MakeSlice returns un-addressable Value

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…
castiel
  • 2,675
  • 5
  • 29
  • 38
1
2 3 4 5 6 7