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.
This commit is contained in:
Damien Neil 2021-10-21 13:27:40 -07:00 committed by GitHub
parent 42db8a3f96
commit c8dfe32b5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 41 additions and 39 deletions

View File

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

View File

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

View File

@ -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]

View File

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

View File

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

View File

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

View File

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

5
examples/go/go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/protocolbuffers/protobuf/examples/go
go 1.14
require google.golang.org/protobuf v1.27.1

6
examples/go/go.sum Normal file
View File

@ -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=