Is it possible to differentiate between []interface{}
and interface{}
in a switch case?
Trying to create a decoding function in which you can pass in different types and then a switch case determines the type and then proceed to decode that particular type. Although I am having issues when the type passed is a []interface{}
.
I have been experimenting with the reflect
package but no luck thus far.
See code snippet and playground link below.
package main
import (
"fmt"
"math/big"
)
type Test struct {
t interface{}
}
func main() {
testVar1 := big.NewInt(0)
testVar2 := int64(1)
testVar3 := []byte("test")
testVar4 := true
testVar5 := []int{1, 2, 3, 4}
var testVar6 Test
Issue(testVar1)
Issue(testVar2)
Issue(testVar3)
Issue(testVar4)
Issue(testVar5)
Issue(testVar6)
}
func Issue(t interface{}) {
switch t.(type) {
case *big.Int:
fmt.Println("*big.Int")
case int8, int16, int32, int64:
fmt.Println("int8, int16, int32, int64")
case []byte:
fmt.Println("[]byte")
case bool:
fmt.Println("bool")
case []interface{}:
fmt.Println("how to get testVar5 to print here")
fmt.Println("[]interface{}")
case interface{}:
fmt.Println("interface{}")
default:
fmt.Println("unsupported type")
}
}
Results:
*big.Int
int8, int16, int32, int64
[]byte
bool
interface{}
interface{}
Is there any way to get testVar5
to hit the []interface{}
case?