-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added final formats in golang cource, GOB and Protobuf
- Loading branch information
Showing
7 changed files
with
232 additions
and
9 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |