dictionary - Convert the dataype of VALUES in Maps Go language -
i have map in go :
var userinputmap = make(map[string]string)
and values in of type :
[abcd:30 efgh:50 pors:60]
not 30,50,60 strings on here.
i wish have same map numeric values should have float64 type instead of string type.
desired output :
var output = make(map[string]float64)
i tried error : cannot use <placeholder_name> (type string) type float64 in assignment
you cannot simple typecasting; 2 maps have different representations in memory.
to solve this, have iterate on every entry of first map, convert string representation of float float64
, store new value in other map:
import "strconv" var output = make(map[string]float64) key, value := range userinputmap { if converted, err := strconv.parsefloat(value, 64); err == nil { output[key] = converted } }
Comments
Post a Comment