Skip to content

Commit

Permalink
Added final formats in golang cource, GOB and Protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
k8s4 committed Oct 29, 2024
1 parent 724dfba commit 30b4ac1
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 9 deletions.
134 changes: 134 additions & 0 deletions examples/golang/cource/06f_formats/examples/gen/person.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/golang/cource/06f_formats/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module 06f_formats

go 1.23.2

require gopkg.in/yaml.v3 v3.0.1 // indirect
require (
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions examples/golang/cource/06f_formats/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
38 changes: 38 additions & 0 deletions examples/golang/cource/06f_formats/gob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"log"
"bytes"
"encoding/gob"
)

func EncodeGob() []byte {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)

m := make(map[string]string)
m["foo"] = "bar"

if err := enc.Encode(m); err != nil {
log.Fatal(err)
}

return buf.Bytes()
}

func DecodeGob(input []byte) {
buf := bytes.NewBuffer(input)
dec := gob.NewDecoder(buf)

m := make(map[string]string)

if err := dec.Decode(&m); err != nil {
log.Fatal(err)
}
fmt.Println(m["foo"])
}

func main() {
DecodeGob(EncodeGob())
}
8 changes: 0 additions & 8 deletions examples/golang/cource/06f_formats/main.go

This file was deleted.

8 changes: 8 additions & 0 deletions examples/golang/cource/06f_formats/person.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax="proto3";

option go_package = "examples/gen;gen";

message Person {
string name = 1;
int32 age = 2;
}
46 changes: 46 additions & 0 deletions examples/golang/cource/06f_formats/protobuf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

/*
Some preparations:
sudo apt install protobuf-compiler
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go get google.golang.org/protobuf/proto
go get google.golang.org/protobuf/reflect/protoreflec
go get google.golang.org/protobuf/runtime/protoimpl
protoc person.proto --go_out=./
*/

import (
"fmt"
"log"
"golang/cource/06f_formats/examples/gen"
"google.golang.org/protobuf/proto"
)

func ProtobufCase() {
pedro := gen.Person{
Name: "Pedro",
Age: 43,
}

data, err := proto.Marshal(&pedro)
if err != nil {
log.Fatal("Marshaling error: %v", err)
}

fmt.Println(data)

otherPedro := &gen.Person{}
err = proto.Unmarshal(data, otherPedro)
if err != nil {
log.Fatal("Unmarshaling error: %v", err)
}

fmt.Println(otherPedro.GetName())
fmt.Println(otherPedro.GetAge())
}


func main() {
ProtobufCase()
}

0 comments on commit 30b4ac1

Please sign in to comment.