aboutsummaryrefslogtreecommitdiff
path: root/examples/list_people.go
diff options
context:
space:
mode:
authorFeng Xiao <xfxyjwf@gmail.com>2015-12-21 00:31:04 -0800
committerFeng Xiao <xfxyjwf@gmail.com>2015-12-21 00:34:19 -0800
commitd21780841734326d8a7653710f4f23b5366c4447 (patch)
tree25df44708c81059a3e1c7025e12b734cc80ee3f4 /examples/list_people.go
parent1e2fece3e01b8e079b4f6930306377a9332a2ddb (diff)
parentb27f2893b2a118b4e3bfcd23d32114dbdd4e6d9b (diff)
downloadprotobuf-d21780841734326d8a7653710f4f23b5366c4447.tar.gz
protobuf-d21780841734326d8a7653710f4f23b5366c4447.tar.bz2
protobuf-d21780841734326d8a7653710f4f23b5366c4447.zip
Merge branch master into v3.0.0-beta-2
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)
}