bound := []interface{}{1.00, 1.00, 1.00, 1.00}
new_bound := bound.([]float32)
log.Println(new_bound)
How convert array of interface to array of float?
invalid type assertion: bound.([]float32) (non-interface type []interface {} on left)
And in real project
panic: interface conversion: interface is []interface {}, not []float32
In your example you have a slice where each item it contains is an interface{}
rather than say, a single interface{}
representing a []float32
, for this reason you can't convert the whole collection so simply like that. Instead you have to iterate it and do a type assertion on each item in the collection. Here's an example; https://play.golang.org/p/dD4161xgaV
bound := []interface{}{1.00, 1.00, 1.00, 1.00}
new_bound := []float64{}
for _, v := range bound {
new_bound = append(new_bound, v.(float64))
}
One other thing to note, those literals have their type implied and it's float64
so you actually need that here.
EDIT: Including this more optimized solution posted by OneOfOne;
func main() {
bound := []interface{}{1.00, 1.10, 1.11, 1.111}
new_bound := make([]float64, len(bound))
for i := range bound {
new_bound[i] = bound[i].(float64)
}
fmt.Println(new_bound)
}
You will need to iterate over your slice and append each item to a new slice. In order to append it to your []float32, you will need to type assert:
bound := []interface{}{1.00, 1.00, 1.00, 1.00}
new_bound := make([]float32, 0, len(bound))
for _, v := range bound {
new_bound = append(new_bound, v.(float32))
}
GoPlay here: https://play.golang.org/p/S8-4KJkqPq