Skip to content

Commit

Permalink
Example #19: serialize to BSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Jul 2, 2020
1 parent 7944c65 commit 4a33d78
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lesson7/marshalling/19_bson_serialize.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}

0 comments on commit 4a33d78

Please sign in to comment.