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
2
votes
1 answer

How to I convert reflect.New's return value back to the original type

I'm using reflection in go and I noticed the oddity expressed below: package main import ( "log" "reflect" ) type Foo struct { a int b int } func main() { t := reflect.TypeOf(Foo{}) log.Println(t)…
Rick Smith
  • 9,031
  • 15
  • 81
  • 85
1
vote
1 answer

Render table in html template where table-data are keys and values of some json data in Golang

In backend I send http request to some third party site and retrieve some json data in response. Keys in the json response are not always same, but I have some idea of what they might be. For example: Sending request to example.com/data/1 could…
1
vote
0 answers

How to flatten a dynamic value in Go?

I am trying to flatten a dynamic value in Go into a slice of key value pairs. So I define a struct as : type KeyValuePair struct { key string value interface{} } For simple values like x = 10, the function should return []KeyValuePair{"x",…
dragonfly02
  • 3,403
  • 32
  • 55
1
vote
1 answer

Can I optimize this Go reflect function so it isn't as slow?

I am working on a project where I need to build out the partial updates for all of the methods that will be supported. Each partial update will require a different struct, with different fields and number of fields, and not knowing which ones will…
JDev
  • 73
  • 1
  • 8
1
vote
0 answers

How to get a single struct with a struct array in reflect

type Foo struct { Bar interface{} Nothing string } type A struct { ... } type B struct { ... } var car Foo In the program, car.Bar maybe []A or []B. I don't know what is real 'Foo' before the program running, when I use…
wan9xy
  • 11
  • 1
1
vote
1 answer

Get pointers to all fields of a struct dynamically using reflection

I'm trying to build a simple orm layer for golang. Which would take a struct and generate the cols [] which can then be passed to sql function rows.Scan(cols...) which takes pointers of fields in the struct corresponding to each of the columns it…
1
vote
1 answer

Reading nested structure using reflection

I write a recursive function that iterate over deep nested struct like the following: type Container struct { Name string Items []Item } type Item struct { Name string Info Info Vals []string } // recursively reads nested…
kostya
  • 27
  • 7
1
vote
1 answer

How to get the element type of slice?

If there is a struct like: type A struct { Arr []int } How can I get the element type in the slice arr? for example, an empty A instance is passed in, how can I get the int type? func PrintElementType(obj interface{}) { objType :=…
Mojito
  • 27
  • 2
1
vote
1 answer

How to use "reflect" to set interface value inside a struct of struct

Had a rough time trying to set the interface value by using "reflect" package. The interface value is actually inside a struct of a struct. See my code in Go Playground Basically, inside initProc, I want to assign dummyAFunc function to DummyA field…
calvinchso
  • 15
  • 5
1
vote
1 answer

How to use reflect to set the value of a map on a struct?

In Go, how can I use the reflect package to set the value of a map? package main import ( "reflect" ) type Mapped struct { M map[string]string } func main() { m := map[string]string{"A":"VA", "B": "VB"} mv := reflect.ValueOf(m) …
Chance
  • 11,043
  • 8
  • 61
  • 84
1
vote
0 answers

Is it possible to provide a Name to a Dynamically created Struct (using Reflection) in Golang

I've a dynamically created struct and I would like to provide a Name to this struct. Is it possible to do that? // Test ... type Test struct { Name string } func main() { structFields := []reflect.StructField{ { Name:…
Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52
1
vote
1 answer

Check if a method type matches a function type

Given the following example, how to check if a method matches a function signature? package main import ( "fmt" "context" "reflect" ) // signature to check type Fn func(context.Context) type testStruct struct {} func (*testStruct)…
CodeBreaker
  • 488
  • 9
  • 18
1
vote
1 answer

Instantiate a new obj using Go reflect and type assert to an interface

I can't explain why the following is working. package main import ( "fmt" "reflect" "strings" ) type MyInterface interface { someFunc() } type Dock struct { } func (d *Dock) someFunc() { } type Group struct { Docks []Dock…
huggie
  • 17,587
  • 27
  • 82
  • 139
1
vote
1 answer

How to set tags while using reflect.New

When creating a new struct from existing struct, tags are not set on the new struct. For example: package main import ( "fmt" "reflect" ) type Foo struct { Bar string `custom:"tag"` } func readTag(e interface{}) { t :=…
CodeBreaker
  • 488
  • 9
  • 18
1
vote
0 answers

Gomock SetArg() panicking

I'm trying to use gomock to mock an interface called SuccessHandler to test a function. The interfaces I have: type Item interface { SetResults(results string) } type SuccessHandler interface { HandleSuccess(item Item) error } And an…
robbieperry22
  • 1,753
  • 1
  • 18
  • 49