typeconv.go
func Convert(val interface{}, targetType reflect.Type) reflect.Value {
	value := reflect.ValueOf(val)
	if !value.IsValid() {
		return reflect.Zero(targetType)
	}

	valType := value.Type()
	if valType.ConvertibleTo(targetType) {
		return value.Convert(targetType)
	}

	//fmt.Printf("Value type is %v, emptyInterfaceType is %v, equals %v\n", valType, emptyInterfaceType, valType == emptyInterfaceType)
	interfaceVal := value.Interface()

	switch realVal := interfaceVal.(type) {
	case float64:
		return reflect.ValueOf(realVal).Convert(targetType)
	case []interface{}:
		// val is of type []interface{}, try to convert to typ
		sliceSize := len(realVal)
		targetSlice := reflect.MakeSlice(targetType, 0, sliceSize)
		elemType := targetType.Elem()
		for i := 0; i < sliceSize; i++ {
			targetSlice = reflect.Append(targetSlice, Convert(value.Index(i), elemType))
		}
		return targetSlice
	}

	panic(fmt.Errorf("convert from type %v to %v failed: %v", valType, targetType, value))
}
func Convert(val interface{}, targetType reflect.Type) reflect.Value {
	value := reflect.ValueOf(val)
	if !value.IsValid() {
		return reflect.Zero(targetType)
	}

	valType := value.Type()
	if valType.ConvertibleTo(targetType) {
		return value.Convert(targetType)
	}

	//fmt.Printf("Value type is %v, emptyInterfaceType is %v, equals %v\n", valType, emptyInterfaceType, valType == emptyInterfaceType)
	interfaceVal := value.Interface()

	switch realVal := interfaceVal.(type) {
	case float64:
		return reflect.ValueOf(realVal).Convert(targetType)
	case []interface{}:
		// val is of type []interface{}, try to convert to typ
		sliceSize := len(realVal)
		targetSlice := reflect.MakeSlice(targetType, 0, sliceSize)
		elemType := targetType.Elem()
		for i := 0; i < sliceSize; i++ {
			targetSlice = reflect.Append(targetSlice, Convert(value.Index(i), elemType))
		}
		return targetSlice
	}

	panic(fmt.Errorf("convert from type %v to %v failed: %v", valType, targetType, value))
}
func Convert ( val interface { } , targetType reflect . Type ) reflect . Value { value := reflect . ValueOf ( val ) if ! value . IsValid ( ) { return reflect . Zero ( targetType ) } valType := value . Type ( ) if valType . ConvertibleTo ( targetType ) { return value . Convert ( targetType ) } //fmt.Printf("Value type is %v, emptyInterfaceType is %v, equals %v\n", valType, emptyInterfaceType, valType == emptyInterfaceType) interfaceVal := value . Interface ( ) switch realVal := interfaceVal . ( type ) { case float64 : return reflect . ValueOf ( realVal ) . Convert ( targetType ) case [ ] interface { } : // val is of type []interface{}, try to convert to typ sliceSize := len ( realVal ) targetSlice := reflect . MakeSlice ( targetType , 0 , sliceSize ) elemType := targetType . Elem ( ) for i := 0 ; i < sliceSize ; i ++ { targetSlice = reflect . Append ( targetSlice , Convert ( value . Index ( i ) , elemType ) ) } return targetSlice } panic ( fmt . Errorf ( " " , valType , targetType , value ) ) }
// try to convert value to target type, panic if fail
try to convert value to target type panic if fail