samedi 23 octobre 2021

Why does MethodByName().Call() return "[

I have the following interface

import (
    "fmt"
    "reflect"
)

type geometry interface {
    Area() float64
    Perimeter() float64
}

func prettyPrint(geometry geometry) {
    geometryType := reflect.TypeOf(geometry)
    fmt.Println(geometryType.Name())
    fmt.Printf("\t%+v\n", geometry)

    for i := 0; i < geometryType.NumMethod(); i++ {
        method := geometryType.Method(i)
        fmt.Println(method.Name)
        fmt.Println(reflect.ValueOf(geometry).MethodByName(method.Name).Call(nil))
    }
}

When calling prettyPrint using a type which implements geometry:

func main() {
    circle := circle{radius: 5}
    prettyPrint(circle)
}

This is the output

circle
        {radius:5}
Area
[<float64 Value>]
Perimeter
[<float64 Value>]

I don't quite understand the reflect.Call() method, or why its printing out each value as [<float64 Value>] - I am trying to get the resulting output from calling the func on the passed in geometry type

I've tried passing in []reflect.Value{} instead of nil as suggested in various places online, but it gives the same results as above

Can anyone shed some light on what exactly is going on here?


I also tried this Invoke method from elsewhere on SO

func Invoke(any interface{}, name string, args ...interface{}) {
    inputs := make([]reflect.Value, len(args))
    for i, _ := range args {
        inputs[i] = reflect.ValueOf(args[i])
    }
    fmt.Println(reflect.ValueOf(any).MethodByName(name).Call(inputs))
}

It gives the same results..

Invoke(circle{}, "Area")

outputs

[<float64 Value>]




Aucun commentaire:

Enregistrer un commentaire