go-reflect Questions
4
Solved
Let's say I want to write a function that finds a value in a slice
I intuitively want to write:
func find(s []interface{}, f func(interface{})bool) int {
for i, item := range s {
if f(item) {
...
Iota asked 22/7, 2016 at 11:57
9
Solved
I'm curious why Go does't implicitly convert []T to []interface{} when it will implicitly convert T to interface{}. Is there something non-trivial about this conversion that I'm missing?
Example:...
Tabling asked 5/10, 2012 at 20:48
3
Solved
having a rough time working with struct fields using reflect package. in particular, have not figured out how to set the field value.
type t struct { fi int; fs string }
var r t = t{ 123, "jblow"...
Airframe asked 18/6, 2011 at 9:24
5
Solved
Here is a simple go program that is not working :
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getProperty(&v, "X"))
}
func g...
Chantay asked 21/9, 2013 at 9:11
8
Solved
Basically, the only way (that I know of) to iterate through the values of the fields of a struct is like this:
type Example struct {
a_number uint32
a_string string
}
//...
r := &Example{(...
Unlawful asked 20/9, 2013 at 21:39
16
Solved
How do I find the type of an object in Go? In Python, I just use typeof to fetch the type of object. Similarly in Go, is there a way to implement the same ?
Here is the container from which I am i...
Analogize asked 24/11, 2013 at 2:8
5
Solved
I have found a function call MethodByName() here http://golang.org/pkg/reflect/#Value.MethodByName but it's not exactly what I want! (maybe because I don't know how to use it ... I cannot find any ...
Cia asked 12/11, 2011 at 9:26
4
Because not all types are comparable, e.g. a slice. So we can't do this
var v ArbitraryType
v == reflect.Zero(reflect.TypeOf(v)).Interface()
Voodooism asked 14/10, 2015 at 3:7
4
Solved
Given a function, is it possible to get its name? Say:
func foo() {
}
func GetFunctionName(i interface{}) string {
// ...
}
func main() {
// Will print "name: foo"
fmt.Println("name:", GetFun...
Subgenus asked 13/8, 2011 at 19:12
7
Solved
I want to check if two structs, slices and maps are equal.
But I'm running into problems with the following code. See my comments at the relevant lines.
package main
import (
"fmt"
"reflect"
)...
Piraeus asked 2/7, 2014 at 14:41
4
Solved
I'm trying to use Go's reflection system to retrieve the name of a function but I get an empty string when calling the Name method on its type. Is this the expected behavior?
This is a simple exam...
Partee asked 24/5, 2012 at 17:51
1
Solved
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 |...
Inoculate asked 20/8, 2021 at 18:24
3
Solved
I have a function named Keys() to get all the keys of a map, here is the code:
func main() {
m2 := map[int]interface{}{
2:"string",
3:"int",
}
fmt.Println(Keys(m2))
}
func Keys(m map[in...
Acidulous asked 22/8, 2016 at 3:57
3
Solved
In Go, how do you check if an object responds to a method?
For example, in Objective-C this can be achieved by doing:
if ([obj respondsToSelector:@selector(methodName:)]) { // if method exists
[...
Burble asked 16/4, 2015 at 19:48
4
Solved
Given the scenario where you have a function which accepts t interface{}. If it is determined that the t is a slice, how do I range over that slice?
func main() {
data := []string{"one","two","th...
Thermochemistry asked 24/12, 2012 at 21:28
1
Solved
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 po...
Iroquoian asked 1/9, 2018 at 22:3
1
Solved
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 ...
Monochromat asked 27/8, 2018 at 18:23
5
Solved
In Go, how do you create the instance of an object from its type at run time? I suppose you would also need to get the actual type of the object first too?
I am trying to do lazy instantiation to ...
Cavafy asked 21/10, 2011 at 13:32
1
Here is a code snippet -
type Gateway struct {
Svc1 svc1.Interface
Svc2 svc2.Interface
}
func (g *Gateway) GetClient(service string) interface{} {
ps := reflect.ValueOf(g)
s := ps.Elem()
f :...
Twoseater asked 15/6, 2017 at 11:25
2
Solved
I can't seem to find it in documentation, is there any guarantee that the order from the fields will match the order declared in the struct? I know it seems like it would logically (due to memory l...
Miley asked 4/9, 2015 at 7:20
3
Solved
I'm working on a templating system written in Go, which means it requires liberal use of the reflect package. In this specific circumstance I need to be able to dynamically call a method on an inte...
Sickle asked 2/1, 2013 at 4:26
1
Solved
Is there a way to use the reflection libraries in Go to go from the name of a type to its Type representation?
I've got a library where the user needs to provide Type representations for some code...
Winfield asked 30/11, 2016 at 3:28
2
Solved
In Go, I would like to do something like this. I have a big object with many structs (using Google's protobuf). here is a contrived example:
person.name = "testing"
person.address.street = "123 te...
Chaussure asked 13/8, 2016 at 16:2
2
Solved
I am trying to create a slice from a reflect.Type. This is what I have so far.
package main
import (
"fmt"
"reflect"
)
type TestStruct struct {
TestStr string
}
func main() {
elemType := re...
Adon asked 7/8, 2016 at 21:54
1
Solved
I need to differentiate such types as
type A []byte
from a []byte. Using reflect, reflect.TypeOf(A{}).Kind tells me that it is a Slice of byte. How can I differentiate []byte{} from A{}, without...
Treillage asked 30/3, 2016 at 13:37
1 Next >
© 2022 - 2024 — McMap. All rights reserved.