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
20
votes
2 answers

Instance new Type (Golang)

Can anyone tell me how to create a new instance of Type from a string? Reflect? There are examples but they are for the older (pre Go 1 versions) of the language [:(]
Accex
  • 351
  • 1
  • 2
  • 11
19
votes
3 answers

Dynamically call method on interface{} regardless of receiver type

I'm working on a templating system written in Go, which means it requires liberal use of the reflect package. In this specific circumstance I need to be able to dynamically call a method on an interface{}. The oddity is that my reflection logic…
Owen Allen
  • 11,348
  • 9
  • 51
  • 63
15
votes
2 answers

Golang: get the type of slice

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…
user2671513
11
votes
4 answers

How to know if a variable of arbitrary type is Zero in Golang?

Because not all types are comparable, e.g. a slice. So we can't do this var v ArbitraryType v == reflect.Zero(reflect.TypeOf(v)).Interface()
v1ct0r
  • 181
  • 3
  • 10
10
votes
1 answer

Identify non builtin-types using reflect

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…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
10
votes
2 answers

Reflect Type.Field() order

I can't seem to find it in documentation, is there any guarantee that the order from the fields will match the order declared in the struct? I know it seems like it would logically (due to memory layout),and it seems to perform this way too, but…
user3591723
  • 1,224
  • 1
  • 10
  • 22
9
votes
2 answers

Golang dynamic variable reference

In Go, I would like to do something like this. I have a big object with many structs (using Google's protobuf). here is a contrived example: person.name = "testing" person.address.street = "123 test st" person.address.city =…
mike
  • 167
  • 2
  • 6
8
votes
2 answers

How to properly use .Call in reflect package

Been having one last issue with my code which involves the .Call function in the reflect package. So I'm making a call such as this: params := "some map[string][]string" in := make([]reflect.Value,0) return_values :=…
user1493543
  • 375
  • 3
  • 5
  • 14
8
votes
3 answers

Go: lookup function by name

I am new to type safe, and can't figure out how to do following package main func test(){ print("In Test") } func main(){ a := "test" a() }
RoboTamer
  • 3,474
  • 2
  • 39
  • 43
7
votes
1 answer

Reflection - method call panics with "call of reflect.Value.Elem on struct Value"

Here is a code snippet - type Gateway struct { Svc1 svc1.Interface Svc2 svc2.Interface } func (g *Gateway) GetClient(service string) interface{} { ps := reflect.ValueOf(g) s := ps.Elem() f :=…
anindyaju99
  • 465
  • 1
  • 5
  • 16
7
votes
1 answer

Selecting a function from a list of functions in Golang

Basically if I have a slice or array of any arbitrary functions, how can I select only the ones that return int, or select only the ones that take ints? I figured that I would need to use the reflect package, but just reading the docs didn't really…
placeybordeaux
  • 2,138
  • 1
  • 20
  • 42
6
votes
1 answer

How to get Type representation from name via reflection?

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)…
jmite
  • 8,171
  • 6
  • 40
  • 81
6
votes
3 answers

How to get keys of map

I have a function named Keys() to get all the keys of a map, here is the code: func main() { m2 := map[int]interface{}{ 2:"string", 3:"int", } fmt.Println(Keys(m2)) } func Keys(m map[interface{}]interface{}) (keys…
GoingMyWay
  • 16,802
  • 32
  • 96
  • 149
6
votes
2 answers

Creating Slice from Reflected Type

I am trying to create a slice from a reflect.Type. This is what I have so far. package main import ( "fmt" "reflect" ) type TestStruct struct { TestStr string } func main() { elemType := reflect.TypeOf(TestStruct{}) elemSlice…
moesef
  • 4,641
  • 16
  • 51
  • 68
6
votes
3 answers

In Go, how can I make a generic function with slices?

Let's say I want to write a function that finds a value in a slice I intuitively want to write: func find(s []interface{}, f func(interface{})bool) int { for i, item := range s { if f(item) { return i } } …
Thomas
  • 8,306
  • 8
  • 53
  • 92