英文:
Generic Programming in Go, Implicit generic type
问题
我需要隐式解析我的结构类型,以便对某些属性进行通用替换。
// 必须用 attValue 替换属性
func SetAttribute(object interface{}, attributeName string, attValue interface{}, objectType reflect.Type) interface{} {
/// 完美运行,但是函数 SetAttribute 需要知道 Customer 类型以进行转换
convertedObject := object.(Customer) // <-- 需要硬编码转换 :(
// 不起作用... 报错!
//convertedObject := object
value := reflect.ValueOf(&convertedObject).Elem()
field := value.FieldByName(attributeName)
valueForAtt := reflect.ValueOf(attValue)
field.Set(valueForAtt)
return value.Interface()
}
请在 Go playground 中查看完整示例...
http://play.golang.org/p/jxxSB5FKEy
英文:
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
得分: 1
convertedObject
是object
接口中的值。对其取地址对原始的customer
没有影响(而且converted
可能是一个不好的前缀,因为它是从“类型断言”生成的,而不是“类型转换”)。
如果直接使用object
,会导致恐慌,因为这样你取到的是接口的地址,而不是customer
的地址。
你需要将要修改的customer
的地址传递给函数:
SetAttribute(&customer, "Local", addressNew, reflect.TypeOf(Customer{}))
你还可以让SetAttribute
函数先检查是否为指针:
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()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论