samedi 23 octobre 2021

Why does type.NumMethod() return 0?

I have this interface

type geometry interface {
    area() float64
    perimeter() float64
}

implemented by circle

import "math"

type circle struct {
    radius float64
}

func (circle circle) area() float64 {
    return math.Pi * circle.radius
}

func (circle circle) perimeter() float64 {
    return 2 * math.Pi * circle.radius
}

but when I pass an instance of circle to my prettyPrint function, NumMethod() returns 0 - why doesn't it count area() and perimeter?

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

    // This prints 0 
    fmt.Println(geometryType.NumMethod())

    for i := 0; i < geometryType.NumMethod(); i++ {
        method := geometryType.Method(i)
        fmt.Println(method.Name)
    }
}

in main func:

circle := circle{radius: 4}
prettyPrint(circle)




Aucun commentaire:

Enregistrer un commentaire