Previous tutorial: Introduction To Protocol Buffers Interface Definition Language - Ii
syntax = "proto3";
package tutorial;
import "google/protobuf/timestamp.proto";
option go_package = "tutorialpb/addressbookpb";
syntax = "proto3";
package tutorial;
import "google/protobuf/timestamp.proto";
option go_package = "tutorialpb/addressbookpb";
message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
google.protobuf.Timestamp last_updated = 5;
}
// Our address book file is just one of these.
message AddressBook {
repeated Person people = 1;
}
apt-get install libprotobuf-dev
apt-get install protobuf-compiler
apt-get install build-essential
tar xvfz protobuf-2.6.0.tar.gz
cd protobuf-2.6.0 $ ./configure && make install
go get -u -v github.com/golang/protobuf/proto
go get -u -v github.com/golang/protobuf/protoc-gen-go
protoc -I=. --go_out=. ./addressbook.proto
package main
import (
"fmt"
"log"
"github.com/golang/protobuf/proto"
pb "github.com/gufranmirza/go-pb/tutorialpb/addressbookpb"
)
func main() {
// Create Addressbook Object with few persons
book := &pb.AddressBook{
People: []*pb.Person{
{
Id: 1234,
Name: "John Doe",
Email: "jdoe@example.com",
Phones: []*pb.Person_PhoneNumber{
{Number: "555-4321", Type: pb.Person_HOME},
},
},
{
Id: 1235,
Name: "Alex",
Email: "alex@example.com",
Phones: []*pb.Person_PhoneNumber{
{Number: "1234-5678", Type: pb.Person_HOME},
},
},
},
}
// let's unmarshal our Address object into byte array so
// that we can use it transfer the data across services
data, err := proto.Marshal(book)
if err != nil {
log.Fatal("marshaling error: ", err)
}
// printing out our raw protobuf object
fmt.Println("Raw data", data)
// let's go the other way and unmarshal
// our byte array into an object we can modify
// and use
addresssBook := pb.AddressBook{}
err = proto.Unmarshal(data, &addresssBook)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}
for _, person := range addresssBook.People {
// print out our `person` object
// for good measure
fmt.Println("===========================")
fmt.Println("ID: ", person.GetId())
fmt.Println("Name: ", person.GetName())
fmt.Println("Email: ", person.GetEmail())
fmt.Println("Phones: ", person.GetPhones())
}
}
go run main.go
Raw data [10 45 10 8 74 111 104 110 32 68 111 101 16 210 9 26 16 106 100 111 101 64 101 120 97
109 112 108 101 46 99 111 109 34 12 10 8 53 53 53 45 52 51 50 49 16 1 10 42 10 4 65 108 101 1
20 16 211 9 26 16 97 108 101 120 64 101 120 97 109 112 108 101 46 99 111 109 34 13 10 9 49 50
51 52 45 53 54 55 56 16 1]
===========================
ID: 1234
Name: John Doe
Email: jdoe@example.com
Phones: [number:"555-4321" type:HOME]
===========================
ID: 1235
Name: Alex
Email: alex@example.com
Phones: [number:"1234-5678" type:HOME]
Final code for this tutorial can be found here : https://github.com/gufranmirza/go-pb
Write your response...