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
1
vote
2 answers

Cannot read StructTag for some reason

I have this handler: func (h Handler) makeGetMany(v PeopleInjection) http.HandlerFunc { type RespBody struct { TypeCreatorMeta string `type:"bar",tc_resp_body_type:"true"` } type ReqBody struct { TypeCreatorMeta string…
user7898461
1
vote
2 answers

Can't set field of a struct that is typed as an interface{}

I've been struggling with the reflect package. This code below does what I expect: package main import ( "reflect" "log" ) type Car struct { Model string } type Person struct { Name string Cars []Car } func ModifyIt(parent…
John
  • 32,403
  • 80
  • 251
  • 422
1
vote
1 answer

reflect: call of reflect.Value.Field on ptr Value

Im trying to map map[string]interface{} to structure. My structure contains pointer types: type A struct{ f1 string f2 *B } type B { f1 string f2 string } when I am trying to iterate through *B, I've got: "reflect: call of…
Pavel
  • 653
  • 2
  • 11
  • 31
1
vote
0 answers

Compile time reflections in Go

I am doing an inventory of all log statements in my Go application. Since the log statements will contain just variable names, a simple grep is not of use. I want to identify the variable "type" in each log statement and if its a struct, the field…
TechCrunch
  • 2,924
  • 5
  • 45
  • 79
1
vote
3 answers

Function that takes different type of struct

I wonder how the following functions lengthAA and lengthBB can be simplified to just one function. Note in those two functions it just compute the length of the array, it is just an example and can be more complicated than that. Ideally, I want…
ryan
  • 974
  • 2
  • 7
  • 13
1
vote
1 answer

How to use an un exported object from third party package as return type in golang?

In my use case I am using influxdb. I am trying to create a global client to influxdb in Golang. But when I return the influxdb client from a function, the client object is not exported in the influxdb package. So I am not able to return this. Here…
Dany
  • 2,692
  • 7
  • 44
  • 67
0
votes
1 answer

Determining the type of an interface {} value returned from a function in Golang

I have a function that returns a value from an enum. The enum definition is as follows: type DataType int64 const ( INT DataType = iota FLOAT STRING BOOL CHAR VOID ERROR BREAK CONTINUE ) func (l…
luisepa1420
  • 63
  • 1
  • 6
0
votes
1 answer

How create type using reflect having pointer receivers

I've two Task struct, namely ShellTask and CmdTask. Created a TaskExecutor Interface and implemented the methods both in ShellTask and CmdTask Pointer receiver. And creating the Task Executor dynamically using reflection at runtime. However when I…
Karthik Prasad
  • 9,662
  • 10
  • 64
  • 112
0
votes
0 answers

golang. reflect makefunc: how to decorate the origin func?

I'm making a tools that decorte my origin function to test my program. It can inject some specific method to oringin function. Now I'm using reflect to do that: I will define a Handle function to decorate the origin func. Like inject error to the…
Pkzzz
  • 101
  • 1
  • 7
0
votes
1 answer

Is there a way to set a pointer struct field to a pointer pointing to the Zero value of that pointer type using reflect?

That was a mouthful of a title, let me explain more. Assuming I have a struct of all pointers (don't know of what type) type A struct { S *string I *int } I want to write a function that takes a pointer to that struct and given a fieldName sets…
strikerdude10
  • 663
  • 6
  • 15
0
votes
2 answers

Append to golang slice passed as empty interface

How to append to empty interface (that has been verified to be a *[]struct)? func main() { var mySlice []myStruct // myStruct can be any struct (dynamic) decode(&mySlice, "...") } func decode(dest interface{}, src string) { // assume dest has…
kent-id
  • 717
  • 10
  • 25
0
votes
1 answer

Read top level struct tags

How to get the tags from a struct field in Go? I have a nested struct which I want to pass as an argument to another function and read the tags there. I know that by accessing it as a field is possible, but I am searching for a way to it. type…
georgeok
  • 5,321
  • 2
  • 39
  • 61
0
votes
0 answers

Get structs in a package for go-gorm migrations

There is a way to get all structs in package (entity in this case) to generate an automatic migrations list? I split entities and migration package and now we have a package dedicated to all entities used by gorm and this is how I manage migration…
0
votes
1 answer

Can a package be an interface in Go?

I'd like to do something like the following in Go, where c/c.go contains: package c import ( a "github.com/.../a" b "github.com/.../b" ) func CallHandler(pkgName string) (err error) { pkg, err := DereferencePackage(pkgName) ... …
Scott Deerwester
  • 3,503
  • 4
  • 33
  • 56
0
votes
1 answer

Using reflect to assign a typed value

I'm working on one of our system applications, specifically in the configuration file handling bits. We currently have 3 different places where a configuration file can be stored, and that can possibly be extended later. What I'm trying to do is…
Ken P
  • 552
  • 3
  • 11