aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/list_people.go
diff options
context:
space:
mode:
Diffstat (limited to 'examples/list_people.go')
-rw-r--r--examples/list_people.go46
1 files changed, 24 insertions, 22 deletions
diff --git a/examples/list_people.go b/examples/list_people.go
index 48b1fbfa..70bc589e 100644
--- a/examples/list_people.go
+++ b/examples/list_people.go
@@ -11,25 +11,29 @@ import (
pb "github.com/google/protobuf/examples/tutorial"
)
-func listPeople(w io.Writer, book *pb.AddressBook) {
- for _, p := range book.People {
- fmt.Fprintln(w, "Person ID:", p.Id)
- fmt.Fprintln(w, " Name:", p.Name)
- if p.Email != "" {
- fmt.Fprintln(w, " E-mail address:", p.Email)
- }
+func writePerson(w io.Writer, p *pb.Person) {
+ fmt.Fprintln(w, "Person ID:", p.Id)
+ fmt.Fprintln(w, " Name:", p.Name)
+ if p.Email != "" {
+ fmt.Fprintln(w, " E-mail address:", p.Email)
+ }
- for _, pn := range p.Phones {
- switch pn.Type {
- case pb.Person_MOBILE:
- fmt.Fprint(w, " Mobile phone #: ")
- case pb.Person_HOME:
- fmt.Fprint(w, " Home phone #: ")
- case pb.Person_WORK:
- fmt.Fprint(w, " Work phone #: ")
- }
- fmt.Fprintln(w, pn.Number)
+ for _, pn := range p.Phones {
+ switch pn.Type {
+ case pb.Person_MOBILE:
+ fmt.Fprint(w, " Mobile phone #: ")
+ case pb.Person_HOME:
+ fmt.Fprint(w, " Home phone #: ")
+ case pb.Person_WORK:
+ fmt.Fprint(w, " Work phone #: ")
}
+ fmt.Fprintln(w, pn.Number)
+ }
+}
+
+func listPeople(w io.Writer, book *pb.AddressBook) {
+ for _, p := range book.People {
+ writePerson(w, p)
}
}
@@ -41,19 +45,17 @@ func main() {
}
fname := os.Args[1]
+ // [START unmarshal_proto]
// Read the existing address book.
in, err := ioutil.ReadFile(fname)
if err != nil {
- if os.IsNotExist(err) {
- fmt.Printf("%s: File not found. Creating new file.\n", fname)
- } else {
- log.Fatalln("Error reading file:", err)
- }
+ log.Fatalln("Error reading file:", err)
}
book := &pb.AddressBook{}
if err := proto.Unmarshal(in, book); err != nil {
log.Fatalln("Failed to parse address book:", err)
}
+ // [END unmarshal_proto]
listPeople(os.Stdout, book)
}