mardi 17 mars 2020

Get function definition by reflection

I was wondering if it was possible to get the underlying definition of a function pass as parameter using reflection.

Let's say I am calling a function like that :

query.MyFunction(func(c interface{}) bool {
    return c.(Car).Name == "lucas"
})

I would like to be able to retrieve this part c.(Car).Name == "lucas" to create a string from it. Using reflection I am able to get the function name, the number of parameters passed and how many out but I can't find a solution to get the code in it

type conditions func(interface{}) bool

func MyFunction(condition conditions) {
    // I don't know how to retrieve the code from the anonymous function
}

If it's not possible using any kind of resources from go, I will find another way to do it without functions

Thank you for your help





Aucun commentaire:

Enregistrer un commentaire