lundi 1 juin 2015

Generic Programming in Go, Implicit generic type

I need Go implicit resolve my type, for generic replacement of some attribute...

//must replace the attribute with attValue
func SetAttribute(object interface{}, attributeName string, attValue interface{}, objectType reflect.Type) interface{} {

    /// works perfectly, but function SetAttribute needs to know Customer type to do the convertion
    convertedObject := object.(Customer) 

    // don't works... raise panic!
    //convertedObject := object 


    value := reflect.ValueOf(&convertedObject).Elem()
    field := value.FieldByName(attributeName)
    valueForAtt := reflect.ValueOf(attValue)
    field.Set(valueForAtt)

    return value.Interface()
}

Please check out full example on Golang play... http://ift.tt/1EP2im5





Aucun commentaire:

Enregistrer un commentaire