FAQ
I am also trying to do type assertion for every case as below but also get
the error as well:

func serialize(value interface{}) string {
     result := "<value>"
     switch asserted_value := value.(type) {
     case string:
       result += fmt.Sprintf("<string>%s</string>", asserted_value)
     case int, int8, int16, int32, int64, uint, uint8, uint16, uint32,
uint64:
       result += fmt.Sprintf("<int>%d</int>", asserted_value)
     case float32, float64:
       result += fmt.Sprintf("<float>%f</float>", asserted_value)
     case map[string]interface{}:
       result += "<struct>"
       for k, v := range asserted_value {
         result += "<member>"
         result += fmt.Sprintf("<name>%s</name>", k)
         result += serialize(v)
         result += "</member>"
       }
       result += "</struct>"
     case map[string]string, map[string]int, map[string]int8,
map[string]int16, map[string]int32, map[string]int64,
       map[string]uint, map[string]uint8, map[string]uint16,
map[string]uint32, map[string]uint64,

       map[string]float32, map[string]float64:
       result += "<struct>"
     for k, v := range asserted_value { << -- error here "cannot range
over asserted_value (type interface {})"
         result += "<member>"
         result += fmt.Sprintf("<name>%s</name>", k)
         result += serialize(v)
         result += "</member>"
       }
       result += "</struct>"

     default:
       log.Fatal("Cannot serialise: ", value)
     }
     result += "</value>"
     return result
   }


On 24 July 2014 16:06, Theeraphol Wattanavekin wrote:

Hi,

I am making a function that getting interface{} as parameter and have
switch type to check type.
The problem comes when I want to loop through any map with any type of
value it has.
But I found the problem that I cannot do that without type assertion.
Is there any other way to do without declaring every case of map with its
specific value types?

Here is my current code:

func serialize(value interface{}) string{


result := "<value>"
switch value.(type) {
case string:
result += fmt.Sprintf("<string>%s</string>", value.(string))
case int:
result += fmt.Sprintf("<int>%d</int>", value)
case map[string]interface{}:
result += "<struct>"
for k, v := range value.(map[string]interface{}) {
result += "<member>"
result += fmt.Sprintf("<name>%s</name>", k)
result += serialize(v)
result += "</member>"
}
result += "</struct>"
default:
log.Fatal("Cannot serialise: ", value)
}
result += "</value>"
return res
}


So, the above code is working fine if I give it map[string]interface{} as
parameter.
But it won't for other map[string]anytype such as "map[string]string",
"map[string]int", etc.

I was thinking about using reflect in default case to check if it is map
and then do for loop but I got stuck there.

Below is the snippet changed from above code only "default" case

default:
tmpVal:=reflect.ValueOf(value)
if tmpVal.Kind() == reflect.Map{
result+="<struct>"
for k, v := range tmpVal { <-- I won't be able to do for loop w/o
doing type assertion correctly but I actually don't know whether map value
is int, string, float, etc...
result +="<member>"
result +=fmt.Sprintf("<name>%s</name>",k)
result += string(v)
result += "</member>"
}
result+="</struct>"
}else{
log.Fatal("Cannot serialise: ", value)
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 5 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 24, '14 at 7:07a
activeJul 24, '14 at 8:01a
posts5
users2
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase