dimanche 5 mars 2017

Go lang reflection how to recognize interface underlining type

For instance :

package main

import (
    "fmt"
    "reflect"
)

func main() {

    arr := []int{}

    var arrI interface{} = arr

    arrValuePtr := reflect.ValueOf(&arrI)
    arrValue := arrValuePtr.Elem()

    fmt.Println("Type: ", arrValue.Type()) // prints: "Type: interface{}
    fmt.Println("Interface value: ", arrValue.Interface()) // prints: "Interface value: []"


    arrValue.Set(reflect.Append(arrValue, reflect.ValueOf(55))) 
    // error: panic: reflect: call of reflect.Append on interface Value
}

So is there a way to recognize that arrValue is a slice value rather than interface{} value? http://ift.tt/2mGhANu





Aucun commentaire:

Enregistrer un commentaire