patterngoCritical
Composite literal uses unkeyed fields
Viewed 0 times
literalcompositeunkeyedusesfields
Problem
I am working with new official mongodb driver for golang. I have created one complex query to insert the data into mongo db and then sort it according to an element value. I am using a filter in which I have created the bson type using :-
But It is showing a warning saying:
primitive.E composite literal uses unkeyed fields
The warnings are creating a mess in my code.
filter := bson.D{{"autorefid", "100"}}But It is showing a warning saying:
primitive.E composite literal uses unkeyed fields
The warnings are creating a mess in my code.
Solution
The warnings can be stopped by setting the check flag to false.
By default all checks are performed. If any flags are explicitly set to true, only those tests are run. Conversely, if any flag is
explicitly set to false, only those tests are disabled. Thus
-printf=true runs the printf check, -printf=false runs all checks except the printf check.
But the warning is due to not providing the keys name when setting the value in
Setting keys for
Package primitive contains types similar to Go primitives for BSON
types can do not have direct Go primitive representations.
E represents a BSON element for a D. It is usually used inside a D.
For more information have a look at primitive.E
$ go doc cmd/vetBy default all checks are performed. If any flags are explicitly set to true, only those tests are run. Conversely, if any flag is
explicitly set to false, only those tests are disabled. Thus
-printf=true runs the printf check, -printf=false runs all checks except the printf check.
Unkeyed composite literals
Flag: -composites
Composite struct literals that do not use the field-keyed syntax.But the warning is due to not providing the keys name when setting the value in
primitive.E struct.Setting keys for
primitive.E struct will remove the warning messages. For examplefilter := bson.D{primitive.E{Key: "autorefid", Value: "100"}}Package primitive contains types similar to Go primitives for BSON
types can do not have direct Go primitive representations.
type E struct {
Key string
Value interface{}
}E represents a BSON element for a D. It is usually used inside a D.
For more information have a look at primitive.E
Code Snippets
$ go doc cmd/vetUnkeyed composite literals
Flag: -composites
Composite struct literals that do not use the field-keyed syntax.filter := bson.D{primitive.E{Key: "autorefid", Value: "100"}}type E struct {
Key string
Value interface{}
}Context
Stack Overflow Q#54548441, score: 102
Revisions (0)
No revisions yet.