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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -