From the mgo
docs:
pipe := collection.Pipe([]bson.M{{"$match": bson.M{"name": "Otavio"}}})
Decomposing this statement, I see that the argument passed to collection.Pipe
is of type []bson.M
. Given that slices are initialized via []sometype{ ... }
syntax, I conclude that the aforementioned slice contains exactly one item (so len == 1
), and that this item is the {"$match": bson.M{"name": "Otavio"}}
literal.
What is the type of this literal? Intuitively, I want to say it's a map[string]interface{}
, but when I tried checking on the go playground, I'm unable to initialize a similar data structure: https://play.golang.org/p/7QKYaQPi6g
My question is therefore twofold:
https://play.golang.org/p/7QKYaQPi6g
(assuming I've made no mistake in my reasoning)x := {"foo": 1, "bar": 1}
?bson.M
is just another name for map[string]interface{}
. See its documentation. Its declared as,
type M map[string]interface{}
So, yes {"$match": bson.M{"name": "Otavio"}
is of type bson.M
x := {"foo": 1, "bar": 1}
In this statement there is no chance to deduct the type. When you do something like this:
[]bson.M{{"$match": bson.M{"name": "Otavio"}}}
Compiler know that you are initializing slice of specific type, so there is no need to repeat this type every time (unless it's interface). This initalisation is called composite literals and was introduced in go1.0