golang反射动态添加切片(数组)

package main

func main(){

	var arr1 = []int{}

	a0 := reflect.ValueOf(&arr1).Elem()

	e0 := make([]reflect.Value, 0)
	e0 = append(e0, reflect.ValueOf(100))
	e0 = append(e0, reflect.ValueOf(200))
	e0 = append(e0, reflect.ValueOf(300))
	e0 = append(e0, reflect.ValueOf(400))
	val_arr1 := reflect.Append(a0, e0...)

	a0.Set(val_arr1)
	fmt.Println(val_arr1)
	fmt.Println(cap(e0))
	fmt.Println(cap(arr1))

}

 

你可能感兴趣的:(GoLang)