2

I'm having a few problems iterating through *T funcs from a struct using reflect.

I've searched a lot of answers but none seems to talk specifically about this situation.

I've found a reflect.NewAt at golang documentation but to be honest I didn't understand, and again I couldn't find a single answer for my situation.

For a better understanding... by having the following struct:

type Counter struct {}
func (self *Counter) Add(n int) {}

If I use reflect by calling the struct pointer itself, it works as expected:

y := reflect.TypeOf(&Counter{})

for k := 0; k < y.NumMethod(); k++ {
    fmt.Println(y.Method(k)) // {Add  func(*Counter, int) <func(*Counter, int) Value> 0}
}

But in my case, multiple structs can arrive here, so it arrives as an interface:

var p interface{} = Counter{}

z := reflect.New(reflect.TypeOf(p))

for k := 0; k < z.NumMethod(); k++ {
   fmt.Println(z.Method(k)) // 0x47d150
}

But as shown, it prints the memory address.

I expect the 0x47d150 to be the same output as I was using the pointer directly. What I'm doing wrong here?

1 Answers1

3

The value y a reflect.Type. The Method method on a type is the equivalent of a method expression.

The value z is a reflect.Value. The Method method on a value is the equivalent of a method value.

The printed representation is different because method expressions and method values are not the same thing.

Use reflect.PtrTo to get pointer type for a type:

var p interface{} = Counter{}
z := reflect.PtrTo(reflect.TypeOf(p))
for k := 0; k < z.NumMethod(); k++ {
    fmt.Println(z.Method(k))  // {Add  func(*Counter, int) <func(*Counter, int) Value> 0}
}

The concrete value in the interface is a non-pointer value. You can use this code if the concrete value in the interface is the pointer type:

var p interface{} = &Counter{}
z := reflect.TypeOf(p)
for k := 0; k < z.NumMethod(); k++ {
    fmt.Println(z.Method(k)) // {Add  func(*Counter, int) <func(*Counter, int) Value> 0}
}
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
  • I'd ask the question of why we're at this level in abstraction in the first place. Unless we're trying to grab stack information? – Lee Jacobs Mar 29 '19 at 05:34
  • I'm asking why one would want to reflect on methods of a type with a known function signature. I feel it's fairly rare to not have your needs met by a proper interface in that instance. – Lee Jacobs Mar 29 '19 at 05:48
  • @LeeJacobs I'm developing a tool that converts Structs to Javascript object. A file containing a []interface{} filled with structs will be iterated and generate the Javascript objects. – alexandremoraes Mar 29 '19 at 12:11
  • @CeriseLimón How the hell I didn't noticed this function anywhere? LOL Thanks brother! – alexandremoraes Mar 29 '19 at 12:12
  • @alexandremoraes why? https://golang.org/pkg/encoding/json/ creating json objects from structs exists. Unless you're creating something more along the lines of transcoding? – Lee Jacobs Mar 30 '19 at 03:34
  • 1
    @LeeJacobs I'm creating a fully transpiled object (including funcs) from a go struct. json.Marshal does not work in this case. – alexandremoraes Mar 31 '19 at 22:35