Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: third_party/protobuf/examples/list_people.go

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 package main
2
3 import (
4 "fmt"
5 "io"
6 "io/ioutil"
7 "log"
8 "os"
9
10 "github.com/golang/protobuf/proto"
11 pb "github.com/google/protobuf/examples/tutorial"
12 )
13
14 func writePerson(w io.Writer, p *pb.Person) {
15 fmt.Fprintln(w, "Person ID:", p.Id)
16 fmt.Fprintln(w, " Name:", p.Name)
17 if p.Email != "" {
18 fmt.Fprintln(w, " E-mail address:", p.Email)
19 }
20
21 for _, pn := range p.Phones {
22 switch pn.Type {
23 case pb.Person_MOBILE:
24 fmt.Fprint(w, " Mobile phone #: ")
25 case pb.Person_HOME:
26 fmt.Fprint(w, " Home phone #: ")
27 case pb.Person_WORK:
28 fmt.Fprint(w, " Work phone #: ")
29 }
30 fmt.Fprintln(w, pn.Number)
31 }
32 }
33
34 func listPeople(w io.Writer, book *pb.AddressBook) {
35 for _, p := range book.People {
36 writePerson(w, p)
37 }
38 }
39
40 // Main reads the entire address book from a file and prints all the
41 // information inside.
42 func main() {
43 if len(os.Args) != 2 {
44 log.Fatalf("Usage: %s ADDRESS_BOOK_FILE\n", os.Args[0])
45 }
46 fname := os.Args[1]
47
48 // [START unmarshal_proto]
49 // Read the existing address book.
50 in, err := ioutil.ReadFile(fname)
51 if err != nil {
52 log.Fatalln("Error reading file:", err)
53 }
54 book := &pb.AddressBook{}
55 if err := proto.Unmarshal(in, book); err != nil {
56 log.Fatalln("Failed to parse address book:", err)
57 }
58 // [END unmarshal_proto]
59
60 listPeople(os.Stdout, book)
61 }
OLDNEW
« no previous file with comments | « third_party/protobuf/examples/list_people.cc ('k') | third_party/protobuf/examples/list_people.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698