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

Assign result of reflect.AppendSlice to pointer

I have troubles translating this piece of code, which is effectively a left rotate on a slice, into a more generic version which accepts interface{} as an input parameter. func rotate(a *[]int, i int) { x, b := (*a)[:i], (*a)[i:] *a =…
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
0
votes
2 answers

Golang convert list objects to string

I have two structs: type A struct { BankCode string `json:"bankCode"` BankName string `json:"bankName"` } And: type B struct { A extra string `json:" extra"` } And two slices: listsA []A and listsB…
star zhang
  • 1
  • 1
  • 4
-1
votes
1 answer

How can I reach struct member in interface type

I have to keep multi type struct in slice and seed them. I took with variadic parameter of interface type and foreach them. If I call the method of interface it works, but when I trying to reach to struct I can't. How can I solve that? Note: Seed()…
icsarisakal
  • 193
  • 7
-1
votes
1 answer

Can I create a function with same signature as another?

I need to create a function that wraps an inner function, and has exactly the same signature as the inner function. I am fearing that this is not possible since Go does not support generics, but perhaps it is achievable using reflect? The following…
bkr879
  • 2,035
  • 2
  • 15
  • 23
-1
votes
1 answer

Strange behavior for a pointer of an interface

I wrote 3 similar functions to figure out a strange behavior of Go's pointer reflection. package main import ( "reflect" "fmt" ) var i interface{} = struct {}{} // i is an interface which points to a struct var ptr *interface{} = &i …
Bob
  • 105
  • 1
  • 12
-1
votes
2 answers

How to check if a function exists in go file

Is there an equivalent in golang to check if a function exists in a package/ go file like it is done in python. mymethod in dir(package)
pretty08
  • 137
  • 3
  • 11
-2
votes
1 answer

Declare type dynamically in GoLang

I have a field struct type: { Name: "fieldA", Type: "string", } and an array of this filed type: [{ Name: "fieldA" Type: "string" }, { Name: "filedB", Type: "int", } ... This array may change or grow later. Now I want to define a new struct type…
user1722361
  • 377
  • 1
  • 4
  • 14
-2
votes
1 answer

Why does reflecting the name (or package path) of the error type cause a panic in Go?

Using reflection to get the name or package path of the error type in Golang causes the program to panic (with panic: runtime error: invalid memory address or nil pointer dereference). What is the reasoning for this behaviour? (Doing the same…
neurotempest
  • 147
  • 5
-2
votes
1 answer

How to compare nested structs, with same fields in child slice fields but in different order

In the following code, a1 and a2 are same. They have same fields but with different orders (Book A and Book B are in different order). When I compare then using DeepEqual() method, the result says they are not equal. How to compare them and get a…
MAK
  • 1,915
  • 4
  • 20
  • 44
-2
votes
1 answer

How to create a variable of dynamic type

I am able to create a variable 'model' of type 'Sample' as follows: type Sample struct { Id int `jsonapi:"attr,id,omitempty"` Name string `jsonapi:"attr,name,omitempty"` } var model Sample // created successfully I am able to create…
MAK
  • 1,915
  • 4
  • 20
  • 44
1 2 3 4 5 6
7