lundi 3 juillet 2023

How to get the name of the embedding struct in Go? [duplicate]

I have a struct Base which is embedded in another struct. I want to access the name of the embedding struct dynamically within a method of Base. Here's an example:

package main

type Base struct{
}

func (b *Base) GetStructName() {
    // want to access which embedding struct called it i.e Test
}

type Test struct {
    Base
}

func main() {
    test := &Test{}
    test.GetStructName()
}

I have tried utilizing the runtime and reflect packages, but unfortunately, they haven't provided the desired outcome.





Aucun commentaire:

Enregistrer un commentaire