Building a Simple Address Book with Protocol Buffers in Go
Protocol Buffers (protobuf) are a language-neutral, platform-neutral, extensible mechanism for serializing structured data. This challenge will guide you through defining a protobuf schema, generating Go code from it, and then using that generated code to serialize and deserialize data representing a simple address book containing names and addresses. This is a fundamental skill for building efficient and interoperable systems.
Problem Description
You are tasked with creating a Go program that utilizes Protocol Buffers to manage an address book. The address book will contain a list of Person entries. Each Person has a name (string) and a list of Address entries. Each Address has a street (string), city (string), and zip (string).
Your program should:
- Define a protobuf schema (
addressbook.proto) that describes the structure of the address book, person, and address. - Generate Go code from the protobuf schema using the
protoccompiler. - Create a Go program that:
- Creates an address book.
- Adds at least two
Personentries to the address book. Each person should have at least twoAddressentries. - Serializes the address book to a byte array.
- Deserializes the byte array back into an address book.
- Prints the names and addresses of each person in the deserialized address book to the console.
Examples
Example 1:
addressbook.proto:
syntax = "proto3";
message Address {
string street = 1;
string city = 2;
string zip = 3;
}
message Person {
string name = 1;
repeated Address addresses = 2;
}
message AddressBook {
repeated Person people = 1;
}
Go Program Output (after running):
Person: John Doe
Address: 123 Main St, Anytown, 12345
Address: 456 Oak Ave, Anytown, 67890
Person: Jane Smith
Address: 789 Pine Ln, Anytown, 13579
Address: 101 Elm Rd, Anytown, 24680
Explanation: The program creates an address book, adds two people (John Doe and Jane Smith), each with two addresses. It then serializes and deserializes the data, and finally prints the contents of the deserialized address book.
Constraints
- The protobuf schema must adhere to proto3 syntax.
- The Go program must correctly serialize and deserialize the address book data.
- The Go program must handle potential errors during serialization and deserialization gracefully (though error handling beyond basic printing is not required for this challenge).
- The program should be reasonably efficient; avoid unnecessary memory allocations.
- The output should be clearly formatted and easy to read.
Notes
- You'll need to install the
protoccompiler and the Go protobuf plugin. Instructions can be found on the official Protocol Buffers website: https://developers.google.com/protocol-buffers - Use the
protoccommand to generate the Go code from youraddressbook.protofile. The command will look something like:protoc --go_out=. addressbook.proto - The
repeatedkeyword in the protobuf schema indicates that a field can contain multiple values (e.g., a person can have multiple addresses). - Consider using the
google.protobuf.Timestamptype for more complex date/time handling if needed, but it's not required for this basic challenge. - Focus on the core functionality of defining, serializing, and deserializing data using Protocol Buffers. Advanced features like error handling and data validation can be added later.