Skip to content

Instantly share code, notes, and snippets.

@divjotarora
Created Feb 10, 2021
Embed
What would you like to do?
omitempty example
package main
import (
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
)
type Zeroer struct {
A int
}
var _ bsoncodec.Zeroer = Zeroer{}
func (z Zeroer) IsZero() bool {
return z.A == 0
}
type NotZeroer struct {
A int
}
type Struct struct {
A int `bson:"a,omitempty"`
B bool `bson:"b,omitempty"`
C []string `bson:"c,omitempty"`
Z Zeroer `bson:"z,omitempty"`
NZ NotZeroer `bson:"nz,omitempty"`
}
func main() {
res, err := bson.Marshal(Struct{})
if err != nil {
panic(err)
}
fmt.Println(bson.Raw(res))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment