Skip to content

examples: update Go example #7217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ clean:
rm -f javac_middleman AddPerson*.class ListPeople*.class com/example/tutorial/*.class
rm -f protoc_middleman addressbook.pb.cc addressbook.pb.h addressbook_pb2.py com/example/tutorial/AddressBookProtos.java
rm -f *.pyc
rm -f protoc_middleman_go tutorial/*.pb.go add_person_go list_people_go go.mod go.sum
rm -f go/tutorialpb/*.pb.go add_person_go list_people_go
rm -f protoc_middleman_dart dart_tutorial/*.pb*.dart
rmdir dart_tutorial 2>/dev/null || true
rmdir tutorial 2>/dev/null || true
Expand All @@ -28,10 +28,9 @@ protoc_middleman: addressbook.proto
protoc $$PROTO_PATH --cpp_out=. --java_out=. --python_out=. addressbook.proto
@touch protoc_middleman

protoc_middleman_go: addressbook.proto
mkdir -p tutorial # make directory for go package
protoc $$PROTO_PATH --go_out=tutorial addressbook.proto
@touch protoc_middleman_go
go/tutorialpb/addressbook.pb.go: addressbook.proto
mkdir -p go/tutorialpb # make directory for go package
protoc $$PROTO_PATH --go_opt=paths=source_relative --go_out=go/tutorialpb addressbook.proto

protoc_middleman_dart: addressbook.proto
mkdir -p dart_tutorial # make directory for the dart package
Expand All @@ -51,21 +50,17 @@ add_person_dart: add_person.dart protoc_middleman_dart

list_people_dart: list_people.dart protoc_middleman_dart

go_mod:
go mod init github.com/protocolbuffers/protobuf/examples
go mod tidy
add_person_go: go/cmd/add_person/add_person.go go/tutorialpb/addressbook.pb.go
cd go && go build -o ../add_person_go ./cmd/add_person

add_person_go: add_person.go protoc_middleman_go go_mod
go build -o add_person_go add_person.go
add_person_gotest: go/tutorialpb/addressbook.pb.go
cd go && go test ./cmd/add_person

add_person_gotest: add_person_test.go add_person_go go_mod
go test add_person.go add_person_test.go
list_people_go: go/cmd/list_people/list_people.go go/tutorialpb/addressbook.pb.go
cd go && go build -o ../list_people_go ./cmd/list_people

list_people_go: list_people.go protoc_middleman_go go_mod
go build -o list_people_go list_people.go

list_people_gotest: list_people.go list_people_go go_mod
go test list_people.go list_people_test.go
list_people_gotest: go/tutorialpb/addressbook.pb.go
cd go && go test ./cmd/list_people

javac_middleman: AddPerson.java ListPeople.java protoc_middleman
javac -cp $$CLASSPATH AddPerson.java ListPeople.java com/example/tutorial/AddressBookProtos.java
Expand Down
22 changes: 9 additions & 13 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,18 @@ scripts) and can be used to create/display an address book data file.

### Go

The Go example requires a plugin to the protocol buffer compiler, so it is not
build with all the other examples. See:
Follow instructions in [../README.md](../README.md) to install protoc. Then
install the Go protoc plugin (protoc-gen-go):

https://github.com/golang/protobuf
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest

for more information about Go protocol buffer support.
The "go install" command will install protoc-gen-go into the GOBIN
directory. You can set the $GOBIN environment variable before
running "go install" to change the install location. Make sure the
install directory is in your shell $PATH.

First, install the Protocol Buffers compiler (protoc).

Then, install the Go Protocol Buffers plugin ($GOPATH/bin must be in your $PATH
for protoc to find it):

go get github.com/golang/protobuf/protoc-gen-go

Build the Go samples in this directory with "make go". This creates the
following executable files in the current directory:
Build the Go samples with "make go". This creates the following
executable files in the current directory:

add_person_go list_people_go

Expand Down
2 changes: 1 addition & 1 deletion examples/addressbook.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ option csharp_namespace = "Google.Protobuf.Examples.AddressBook";
// [END csharp_declaration]

// [START go_declaration]
option go_package = "../tutorial";
option go_package = "github.com/protocolbuffers/protobuf/examples/go/tutorialpb";
// [END go_declaration]

// [START messages]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"os"
"strings"

"github.com/golang/protobuf/proto"
pb "github.com/protocolbuffers/protobuf/examples/tutorial"
pb "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
"google.golang.org/protobuf/proto"
)

func promptForAddress(r io.Reader) (*pb.Person, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"strings"
"testing"

"github.com/golang/protobuf/proto"
pb "github.com/protocolbuffers/protobuf/examples/tutorial"
pb "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
"google.golang.org/protobuf/proto"
)

func TestPromptForAddressReturnsAddress(t *testing.T) {
Expand Down Expand Up @@ -51,7 +51,7 @@ unknown
}
for i := 0; i < phones; i++ {
if !proto.Equal(got.Phones[i], want[i]) {
t.Errorf("want phone %q, got %q", *want[i], *got.Phones[i])
t.Errorf("want phone %q, got %q", want[i], got.Phones[i])
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"log"
"os"

"github.com/golang/protobuf/proto"
pb "github.com/protocolbuffers/protobuf/examples/tutorial"
pb "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
"google.golang.org/protobuf/proto"
)

func writePerson(w io.Writer, p *pb.Person) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

pb "github.com/protocolbuffers/protobuf/examples/tutorial"
pb "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
)

func TestWritePersonWritesPerson(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions examples/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/protocolbuffers/protobuf/examples/go

go 1.14

require google.golang.org/protobuf v1.27.1
6 changes: 6 additions & 0 deletions examples/go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=