mardi 2 juin 2020

How to write a function that can get any struct and return the name of the struct?

I tried a function like this:

func getStuctName(aStruct interface{}) string {
    typeof := reflect.TypeOf(aStruct)

    if typeof.Kind() == reflect.Ptr {
         typeof = typeof.Elem()
    }

    return typeof.Name()
}

And I have a blank result: ""

When I call my function with a pointer of a struct.

func callerFunction(aStruct interface{})  {
    fmt.Println(getStructName(&aStruct))
}




Aucun commentaire:

Enregistrer un commentaire