|  | @@ -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)
 | 
	
		
			
				|  |  |  }
 |