dongpouda6700
dongpouda6700
2015-06-01 18:47
采纳率: 100%
浏览 49
已采纳

Go中的泛型编程,隐式泛型

I need Go to implicitly resolve my struct type, in order to do 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) // <-- Need to hard code a cast :(

    // doesn'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 in the Go playground... http://play.golang.org/p/jxxSB5FKEy

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglu1971
    donglu1971 2015-06-01 19:05
    已采纳

    convertedObject is the value of what is in the object interface. Taking the address of that has no effect on the original customer. (and converted is probably a poor prefix for the name, since that is generated from a "type assertion", not a "type conversion")

    If you use object directly, it panics, because you're then taking the address of the interface, not the customer.

    You need to pass the address of the customer you want to modify to the function:

    SetAttribute(&customer, "Local", addressNew, reflect.TypeOf(Customer{}))
    

    You can also have your SetAttribute check if it's a pointer first:

    if reflect.ValueOf(object).Kind() != reflect.Ptr {
        panic("need a pointer")
    }
    
    value := reflect.ValueOf(object).Elem() 
    field := value.FieldByName(attributeName)
    valueForAtt := reflect.ValueOf(attValue)
    field.Set(valueForAtt)
    return value.Interface()
    
    点赞 评论

相关推荐