Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
9e1777f4
Commit
9e1777f4
authored
Dec 17, 2015
by
Feng Xiao
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1052 from tswast/master
Add region tags to the Go protobuf examples.
parents
afbc89a2
7df1d773
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
22 deletions
+53
-22
add_person.go
examples/add_person.go
+5
-0
list_people.go
examples/list_people.go
+24
-22
list_people_test.go
examples/list_people_test.go
+24
-0
No files found.
examples/add_person.go
View file @
9e1777f4
...
...
@@ -105,7 +105,10 @@ func main() {
log
.
Fatalln
(
"Error reading file:"
,
err
)
}
}
// [START marshal_proto]
book
:=
&
pb
.
AddressBook
{}
// [START_EXCLUDE]
if
err
:=
proto
.
Unmarshal
(
in
,
book
);
err
!=
nil
{
log
.
Fatalln
(
"Failed to parse address book:"
,
err
)
}
...
...
@@ -116,6 +119,7 @@ func main() {
log
.
Fatalln
(
"Error with address:"
,
err
)
}
book
.
People
=
append
(
book
.
People
,
addr
)
// [END_EXCLUDE]
// Write the new address book back to disk.
out
,
err
:=
proto
.
Marshal
(
book
)
...
...
@@ -125,4 +129,5 @@ func main() {
if
err
:=
ioutil
.
WriteFile
(
fname
,
out
,
0644
);
err
!=
nil
{
log
.
Fatalln
(
"Failed to write address book:"
,
err
)
}
// [END marshal_proto]
}
examples/list_people.go
View file @
9e1777f4
...
...
@@ -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
)
}
examples/list_people_test.go
View file @
9e1777f4
...
...
@@ -8,6 +8,30 @@ import (
pb
"github.com/google/protobuf/examples/tutorial"
)
func
TestWritePersonWritesPerson
(
t
*
testing
.
T
)
{
buf
:=
new
(
bytes
.
Buffer
)
// [START populate_proto]
p
:=
pb
.
Person
{
Id
:
1234
,
Name
:
"John Doe"
,
Email
:
"jdoe@example.com"
,
Phones
:
[]
*
pb
.
Person_PhoneNumber
{
{
Number
:
"555-4321"
,
Type
:
pb
.
Person_HOME
},
},
}
// [END populate_proto]
writePerson
(
buf
,
&
p
)
got
:=
buf
.
String
()
want
:=
`Person ID: 1234
Name: John Doe
E-mail address: jdoe@example.com
Home phone #: 555-4321
`
if
got
!=
want
{
t
.
Errorf
(
"writePerson(%s) =>
\n\t
%q, want %q"
,
p
.
String
(),
got
,
want
)
}
}
func
TestListPeopleWritesList
(
t
*
testing
.
T
)
{
buf
:=
new
(
bytes
.
Buffer
)
in
:=
pb
.
AddressBook
{[]
*
pb
.
Person
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment