Skip to content

Instantly share code, notes, and snippets.

@cihangir
Created May 30, 2013 07:52
Show Gist options
  • Save cihangir/5676347 to your computer and use it in GitHub Desktop.
Save cihangir/5676347 to your computer and use it in GitHub Desktop.
switch / reflect Go
//get type of current value
typeOfKey := reflect.TypeOf(v).String()
//every v, will be a string at the end
switch typeOfKey {
//if we have an complex type, resolve it
case "map[string]interface {}":
//convert value to its real type
tempVal := v.(map[string]interface{})
//we have a lot more to-do, call recursively
src := ConvertTo2DMap(k, tempVal)
//get result and map while iterating
for mapKey, mapValue := range src {
//e.g.=> pages.0.items.1.id : 50e61fc71964f6a837000003
tempKey := k + "." + mapKey
result[tempKey] = mapValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment