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

How to reflect struct recursive in golang

I want to reflect the struct type and value recursively, but it fails. I don't know how to pass the sub struct recursively. It error the following. panic: reflect: NumField of non-struct type goroutine 1…
firelyu
  • 2,102
  • 5
  • 25
  • 29
4
votes
1 answer

Get the length of a slice of unknown type

Let's say I have a function that returns an interface{}. But I know that item returns is a slice of some kind. How can I determine the length of that slice? Here's sample code of what I tried, but they all cause compilation error. package…
John
  • 32,403
  • 80
  • 251
  • 422
4
votes
1 answer

How to get size of struct containing data structures in Go?

I'm currently trying to get the size of a complex struct in Go. I've read solutions that use reflect and unsafe, but neither of these help with structs that contain arrays or maps (or any other field that's a pointer to an underlying data…
dominic
  • 127
  • 1
  • 11
3
votes
1 answer

what does reflect.TypeOf((*error)(nil)).Elem()` mean?

func (s *service) registerMethods() { s.method = make(map[string]*methodType) for i := 0; i < s.typ.NumMethod(); i++ { method := s.typ.Method(i) mType := method.Type if mType.NumIn() != 3 || mType.NumOut() != 1 { …
Wei Wong
  • 65
  • 1
  • 7
3
votes
0 answers

Using reflect, how to check if an embedding type overrides a method of the embedded type?

With embedded types, how can I distinguish using package reflect if a type is implementing a given interface through the embedded type or if it has its own implementation of the method that overrides the implementation of the embedded type? Concrete…
dolmen
  • 8,126
  • 5
  • 40
  • 42
3
votes
1 answer

How to get a list of a struct's methods in Go?

I have a library in which there are both Client and MockClient structs which both implement the same ClientInterface interface. I would like to write unit tests to keep these structs in sync so that they not only just implement the interface, but so…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
3
votes
1 answer

Get all info about a package via importer and reflect?

I stumbled across this answer to a question on how to get all the types in a package: https://stackoverflow.com/a/32142080/470339 Which works great. However, the next step I'd like to take is then iterate over each of these types to get, for…
Alastair
  • 5,894
  • 7
  • 34
  • 61
3
votes
1 answer

How can I type assert a reflect.Value struct back to an interface that I know it implements?

I have recently been writing some auditing processes in go. Most of this is just reflection ripping arbitrary constructs apart. There exists an interface that can be used for object equality. Not all of the things being ripped apart implement this…
lmunro
  • 33
  • 5
2
votes
0 answers

How to create a new instance of a struct that is read from file

I know this may seem like bad design (and I wish I didn't need to do this), but I need to read a struct that is generated automatically at run time and create a new instance of it. The struct that I create I only need limited metadata of so that it…
Charlie Clarke
  • 177
  • 1
  • 9
2
votes
1 answer

go reflect: how to dynamically create a pointer to a pointer to ...?

I would like to create a reflect.Value that represents a multiple-level nested pointer to a final value. The nesting level is not known at compile time. How can I create pointers to pointers using reflect? I already stumble at the "unaddressable…
TheDiveO
  • 2,183
  • 2
  • 19
  • 38
2
votes
3 answers

How to check that a return value of a function satisfies the error interface

I'd like to write some code which inspects the methods of a struct and makes certain assertions on them, for example, that the last thing returned by them should be an error. I've tried the following example script: import ( "context" …
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
2
votes
2 answers

Determine if type is a string using reflect

Some of the existing answers on here about how to determine the type of an object at runtime..god help us if reflect.TypeOf(err) == string { } that doesn't compile if reflect.TypeOf(err) == "string" { } neither does that or this: if…
user5047085
2
votes
1 answer

v.Elem() Vs Indirect(v) when passing in result of a reflect.New(Type)

My question is related to this question here: golang - Elem Vs Indirect in the reflect package Basically it claims that the expression below is true if someX is a reflect.Value that contains a pointer reflect.Indirect(reflect.ValueOf(someX)) ===…
John
  • 32,403
  • 80
  • 251
  • 422
2
votes
1 answer

Unmarshalling Entities from multiple JSON arrays without using reflect or duplicating code

I'm making an JSON API wrapper client that needs to fetch paginated results, where the URL to the next page is provided by the previous page. To reduce code duplication for the 100+ entities that share the same response format, I would like to have…
nijm
  • 2,158
  • 12
  • 28
2
votes
3 answers

How to reflect.New a slice and reflect.AppendSlice it to a source slice

I want to reflect.New an []interface{} like []int and append it into another slice. My code must have an error, but I don't know how to make it right and how to understand deeply the reflect.New and reflect.AppendSlice usage. package main import ( …
weizhao
  • 183
  • 3
  • 16