Skip to content

Instantly share code, notes, and snippets.

@divjotarora
Created February 10, 2021 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divjotarora/d518ed4aa70df5cad1c4e209da1825f0 to your computer and use it in GitHub Desktop.
Save divjotarora/d518ed4aa70df5cad1c4e209da1825f0 to your computer and use it in GitHub Desktop.
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