type M map[string]interface{}
A named and an unnamed type are always different.
doc = bson.M( doc["data"].(map[string]interface{}) )it works and is exactly the same operation as
IMHO 'doc = bson.M( doc["data"].(map[string]interface{}) )' is not okay because it lacks error handling.
The reason for this is that you use a type assertion [1], which can cause a panic at runtime, and a type conversion [2], which doesn't require error handling as an error will generate a compiler error.
My recommendation is to use the type assertion, check for an error and then do the type conversion:
d, ok := doc["data"].(map[string]interface{})
if !ok {
// Error handling
}
doc = bson.M(d)
Why the rules for type assertions don't allow the shorter 'doc, ok := doc["data"].(bson.M)' directly I'm not sure either. Maybe to keep the type assertion rules simple and easy to understand. But maybe there is more to it...
[1] https://golang.org/ref/spec#Type_assertions
[2] https://golang.org/ref/spec#Conversions
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/2YR-EuM2-Po/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.