aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/x/xreflect/assign.go
blob: 624612575c89a5c8f73ece6961a01d595fc8559c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package xreflect

import "reflect"

// Assign source's value to target's value it points to. Source must be value, target must be pointer to existing value.
// Source must be assignable to target's value it points to.
func Assign(source interface{}, target interface{}) bool {
	val := reflect.ValueOf(target)
	typ := val.Type()
	targetType := typ.Elem()
	if reflect.TypeOf(source).AssignableTo(targetType) {
		val.Elem().Set(reflect.ValueOf(source))
		return true
	}

	return false
}