From 8be60259ebca929a3d2691a24bc44ccc90779002 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Fri, 3 Jul 2020 18:45:07 +0200 Subject: [PATCH] Example #20: deserialize from BSON format --- lesson7/marshalling/20_bson_deserialize.go | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lesson7/marshalling/20_bson_deserialize.go diff --git a/lesson7/marshalling/20_bson_deserialize.go b/lesson7/marshalling/20_bson_deserialize.go new file mode 100644 index 0000000..e5d2a52 --- /dev/null +++ b/lesson7/marshalling/20_bson_deserialize.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "gopkg.in/mgo.v2/bson" + "io/ioutil" +) + +type User struct { + Id uint32 + Name string + Surname string +} + +func main() { + var user User + + bsonInput, err := ioutil.ReadFile("1.bson") + if err != nil { + fmt.Println(err) + return + } + + fmt.Printf("Read %d bytes\n", len(bsonInput)) + + err = bson.Unmarshal(bsonInput, &user) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("Deserialized value") + fmt.Println(user) +}