examples: update Go example (#7217)
Update the Go example to use Go modules: Move the example into a
directory containing a go.mod file, change the installation instructions
to use "go install".
Update the go_package option in addressbook.proto to an actual module path.
Update examples to use the google.golang.org/protobuf/proto module.
diff --git a/examples/Makefile b/examples/Makefile
index 8ed2492..1c7ec8d 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -16,7 +16,7 @@
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
@@ -28,10 +28,9 @@
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
@@ -51,21 +50,17 @@
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
diff --git a/examples/README.md b/examples/README.md
index 4bf7c17..a99883e 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -91,22 +91,18 @@
### 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
diff --git a/examples/addressbook.proto b/examples/addressbook.proto
index 5bb3577..1bff4ad 100644
--- a/examples/addressbook.proto
+++ b/examples/addressbook.proto
@@ -24,7 +24,7 @@
// [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]
diff --git a/examples/add_person.go b/examples/go/cmd/add_person/add_person.go
similarity index 96%
rename from examples/add_person.go
rename to examples/go/cmd/add_person/add_person.go
index 7ffb0ab..5d2f21c 100644
--- a/examples/add_person.go
+++ b/examples/go/cmd/add_person/add_person.go
@@ -9,8 +9,8 @@
"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) {
diff --git a/examples/add_person_test.go b/examples/go/cmd/add_person/add_person_test.go
similarity index 88%
rename from examples/add_person_test.go
rename to examples/go/cmd/add_person/add_person_test.go
index d35f10e..f10c355 100644
--- a/examples/add_person_test.go
+++ b/examples/go/cmd/add_person/add_person_test.go
@@ -4,8 +4,8 @@
"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) {
@@ -51,7 +51,7 @@
}
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])
}
}
diff --git a/examples/list_people.go b/examples/go/cmd/list_people/list_people.go
similarity index 91%
rename from examples/list_people.go
rename to examples/go/cmd/list_people/list_people.go
index 6c2c34a..5ca0dcf 100644
--- a/examples/list_people.go
+++ b/examples/go/cmd/list_people/list_people.go
@@ -7,8 +7,8 @@
"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) {
diff --git a/examples/list_people_test.go b/examples/go/cmd/list_people/list_people_test.go
similarity index 97%
rename from examples/list_people_test.go
rename to examples/go/cmd/list_people/list_people_test.go
index aceabd4..b116c16 100644
--- a/examples/list_people_test.go
+++ b/examples/go/cmd/list_people/list_people_test.go
@@ -5,7 +5,7 @@
"strings"
"testing"
- pb "github.com/protocolbuffers/protobuf/examples/tutorial"
+ pb "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
)
func TestWritePersonWritesPerson(t *testing.T) {
diff --git a/examples/go/go.mod b/examples/go/go.mod
new file mode 100644
index 0000000..ed43328
--- /dev/null
+++ b/examples/go/go.mod
@@ -0,0 +1,5 @@
+module github.com/protocolbuffers/protobuf/examples/go
+
+go 1.14
+
+require google.golang.org/protobuf v1.27.1
diff --git a/examples/go/go.sum b/examples/go/go.sum
new file mode 100644
index 0000000..9f8c064
--- /dev/null
+++ b/examples/go/go.sum
@@ -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=