assign.go 484 B

1234567891011121314151617
  1. package xreflect
  2. import "reflect"
  3. // Assign source's value to target's value it points to. Source must be value, target must be pointer to existing value.
  4. // Source must be assignable to target's value it points to.
  5. func Assign(source interface{}, target interface{}) bool {
  6. val := reflect.ValueOf(target)
  7. typ := val.Type()
  8. targetType := typ.Elem()
  9. if reflect.TypeOf(source).AssignableTo(targetType) {
  10. val.Elem().Set(reflect.ValueOf(source))
  11. return true
  12. }
  13. return false
  14. }