Go语言中的泛型编程,隐式泛型类型。

huangapple go评论77阅读模式
英文:

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

convertedObjectobject接口中的值。对其取地址对原始的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()

huangapple
  • 本文由 发表于 2015年6月2日 02:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/30580941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定