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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -