j:=1
Kind
of j
is reflect.Int
, as expected.
var j interface{} = 1
Kind
of j
is also reflect.Int
.
Which value’s kind is reflect.Interface
?
j:=1
Kind
of j
is reflect.Int
, as expected.
var j interface{} = 1
Kind
of j
is also reflect.Int
.
Which value’s kind is reflect.Interface
?
If you're looking for a practical solution, the answer is simple and annoying. reflect.TypeOf
takes an empty interface type into which you put whatever data you want to pass. The problem with this is that an interface type can't hold another interface type, which means that you effectively can't pass an interface to reflect.TypeOf
. There is a workaround, but it's a bit painful. What you have to do is make a composite type (like a struct or slice or map) where one of the element types is an interface type, and extract it. For example:
var sliceOfEmptyInterface []interface{}
var emptyInterfaceType = reflect.TypeOf(sliceOfEmptyInterface).Elem()
That first creates a reflect.Type
representation of []interface{}
(a slice of interface{}
types) and then extracts the element type, which is interface{}
.
Courtesy of this post.