我正在尝试创建一个函数,它可以像这样重置传递的切片:
func resetSlice(slice interface{}) {
v := reflect.ValueOf(slice)
s := v.Type().Elem()
// QUESTION: How to reset the slice here?
}
usernames := []string{"Hello", "World"}
resetSlice(&usernames)
fmt.Println(usernames) // OUTPUT : [Hello World]
// EXPECTED: []
但我不知道如何重置指针切片。创建一个与指针切片具有相同类型的新切片
reflect.New(v.Type().Elem())
然后替换指针切片?但是怎么做呢?