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

How to construct slice of structs dynamically using reflection

I was trying to construct slice of Book structs with pointers but I was unable to get it work with reflection in Go. []*Book slice of Book struct pointers , please note that scanResults method might receive any type of slice and not only Book…
Mozhi
  • 757
  • 1
  • 11
  • 28
0
votes
1 answer

How to create a new function from another function using reflection

Using this code as a template package main import "fmt" type myStruct struct { Value int } type counter int func newFuncHandler(fn func(myStruct) error) (interface{}, *counter) { count := counter(0) newFn := func(e myStruct) error…
CodeBreaker
  • 488
  • 9
  • 18
0
votes
1 answer

Iterate through a struct with an embedded struct

I have several structs that are receiving data. All structs shall include some of the same data, which have been embedded with the HeaderData struct. The data is not filled at the same time, and I need a function to check if all the fields have…
Jimbo
  • 43
  • 6
0
votes
0 answers

Insert data with Gorm with reflect

I'm creating a basic REST service. My intent is to write the logic for the resources as abstractly as possible. What I mean is if I have already created a CRUD logic for endpoint /devices for example, then when I need a new resource endpoint like…
huggie
  • 17,587
  • 27
  • 82
  • 139
0
votes
0 answers

Is there a way to extract package path from reflect.Func?

When reflect.TypeOf is used on functions, resultant object has empty PkgPath(). Full example showing this is as follows: // file 1, package foo: func New() *Foo { return nil } // file 2, package main, main func: typ :=…
Alex
  • 2,916
  • 3
  • 22
  • 27
0
votes
1 answer

Programmatically fill golang struct

I have a file with many types of data record which I need to parse into structs. I'd be grateful to learn of a idiomatic way -- if it exists -- of filling structs by record type. Something like python's namedtuple(*fields) constructor. package…
rorycl
  • 1,344
  • 11
  • 19
0
votes
1 answer

How to list unknown fields after json parsing

Imagine we have following Go structs: type Config struct { Name string `json:"name,omitempty"` Params []Param `json:"params,omitempty"` } type Param struct { Name string `json:"name,omitempty"` Value string…
yname
  • 2,189
  • 13
  • 23
0
votes
1 answer

In go, why are both the reflected value and its interface the same when printed?

Excerpt from the Laws of Reflection: (Why not fmt.Println(v)? Because v is a reflect.Value; we want the concrete value it holds.) This confuses me because the following code: var x float64 = 3.4 var v = reflect.ValueOf(x) fmt.Println("value of…
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
0
votes
1 answer

Using reflect to update value by reference when argument is not a pointer in go

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…
John
  • 32,403
  • 80
  • 251
  • 422
0
votes
0 answers

using a map which store struct field name and type to recover Go struct

Playground for my problem: https://play.golang.org/p/rVwEaxpkJGL I have struct like type SimpleBbInput struct { MyInput struct { Num struct { val int } } HisInput string HerInput uint8 } I will store the field name and…
icelemon
  • 827
  • 2
  • 11
  • 23
0
votes
1 answer

Converting Value to a Struct

I have a Struct which is as follows type pathObject struct { s *gizmo.Session finals bool path *path.Path } I'm getting this struct and I need to extract the path object but I keep getting an compilation error Here is my code for…
allthenutsandbolts
  • 1,513
  • 1
  • 13
  • 34
0
votes
2 answers

Deepcopying struct having pointer-to 0 value in golang

I have a struct in golang as below type Test struct { prop *int } I want to take deepcopy of the struct object when prop is pointer-to zero value. The real struct has lot more fields in it and I want deepcopy of entire struct obj. I tried to…
Rohanil
  • 1,717
  • 5
  • 22
  • 47
0
votes
2 answers

Make slice of a struct with a variable of it

In my func I have a variable of Product struct but I have not access to Product struct and I want make a slice of Product from it's variable for example: test1 := Product{} .... .... .... test2 := []TypeOf(test1) how can I do that? Update: what I…
0
votes
2 answers

Two-way-binding for golang structs

TLDR: Can I register callback functions in golang to get notified if a struct member is changed? I would like to create a simple two-way-binding between a go server and an angular client. The communication is done via websockets. Example: Go: type…
maja
  • 17,250
  • 17
  • 82
  • 125
0
votes
0 answers

Golang - Mapping JSON using reflect and recursion

I am trying to map a json data into map[string]interface{} using reflect and recursion . The function works like expected but it is not mapping all the value when it comes to nested json. I have this following json structure: { "r_id" : "123456", …
sh0umik
  • 1,549
  • 2
  • 17
  • 27