From 4a33d78e7c3008a7bca25d75a8985a1b71cd66b8 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Thu, 2 Jul 2020 08:03:41 +0200 Subject: [PATCH] Example #19: serialize to BSON format --- lesson7/marshalling/19_bson_serialize.go | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lesson7/marshalling/19_bson_serialize.go diff --git a/lesson7/marshalling/19_bson_serialize.go b/lesson7/marshalling/19_bson_serialize.go new file mode 100644 index 0000000..79cf709 --- /dev/null +++ b/lesson7/marshalling/19_bson_serialize.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "gopkg.in/mgo.v2/bson" + "io/ioutil" +) + +type User struct { + Id uint32 + Name string + Surname string +} + +func main() { + user := User{ + 1, + "Pepek", + "Vyskoč"} + + var bsonOutput []byte + + bsonOutput, err := bson.Marshal(user) + if err != nil { + fmt.Println(err) + } else { + fmt.Printf("Encoded into %d bytes\n", len(bsonOutput)) + err := ioutil.WriteFile("1.bson", bsonOutput, 0644) + if err != nil { + fmt.Println(err) + } else { + fmt.Println("And stored into file") + } + } +}