Reputation: 13261
I have a data structure which comes of out go-spew looking like this:
([]interface {}) (len=1 cap=1) {
(string) (len=1938) "value"
}
It is of type []interface {}
How can I print this value
with fmt
, or access it in some way so that I can use it.
Upvotes: 1
Views: 1328
Reputation: 46442
You can use type assertions or reflection work with the generic interface{}
to an underlying type. How you do this depends on your particular use case. If you can expect the interface{}
to be a []interface{}
as in your example, you can:
if sl, ok := thing.([]interface{}); ok {
for _, val := range sl {
fmt.Println(val)
// Or if needed, coerce val to its underlying type, e.g. strVal := val.(string)
}
}
If you can't make assumptions about the underlying type, you'll need to do some black magic using reflect
.
Upvotes: 1