jeudi 4 juin 2015

Go reflect.MakeFunc. How to return a err=nil as reflect.Value?

How to return a err=nil as reflect.Value? I need to write a swap function to use with reflect.MakeFunc().

//my swap implementation, that call the original function and cache results
func swapFunc(ins []reflect.Value) []reflect.Value {
    //call original FindBestOffer and cache results
    //outs := reflect.ValueOf(FindBestOffer).Call(ins)

    //When original function call succeed, i need to 
    //cache the first value (Offer) only.

    //After that, my function needs to return the Offer from 
    //cache and default values for other two returns (bool, error)

    outs := make([]reflect.Value, 3) //mock cache return

    outs[0] = reflect.ValueOf(Offer{10, "cached offer", 20})
    outs[1] = reflect.ValueOf(true)
    outs[2] = reflect.ValueOf(nil).Elem() // --> how to create a default nil error as reflect.Value ???


    return outs
}

Go Playground full example





Aucun commentaire:

Enregistrer un commentaire