Code below will raise a runtime error when append reflect.Value
of nil
:
package main
import (
"fmt"
"reflect"
)
func main() {
var list []interface{}
v := reflect.ValueOf(list)
v = reflect.Append(v, reflect.ValueOf(1)) // [1]
v = reflect.Append(v, reflect.ValueOf("1")) // [1, 1]
v = reflect.Append(v, reflect.ValueOf(nil)) // runtime error
fmt.Println(v)
}
So
- why there is a runtime error?
- how can I use
reflect.Append
to add anil
tointerface{}
slice?
Aucun commentaire:
Enregistrer un commentaire