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:
parent
42db8a3f96
commit
c8dfe32b5a
@ -16,7 +16,7 @@ clean:
|
|||||||
rm -f javac_middleman AddPerson*.class ListPeople*.class com/example/tutorial/*.class
|
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 protoc_middleman addressbook.pb.cc addressbook.pb.h addressbook_pb2.py com/example/tutorial/AddressBookProtos.java
|
||||||
rm -f *.pyc
|
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
|
rm -f protoc_middleman_dart dart_tutorial/*.pb*.dart
|
||||||
rmdir dart_tutorial 2>/dev/null || true
|
rmdir dart_tutorial 2>/dev/null || true
|
||||||
rmdir 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
|
protoc $$PROTO_PATH --cpp_out=. --java_out=. --python_out=. addressbook.proto
|
||||||
@touch protoc_middleman
|
@touch protoc_middleman
|
||||||
|
|
||||||
protoc_middleman_go: addressbook.proto
|
go/tutorialpb/addressbook.pb.go: addressbook.proto
|
||||||
mkdir -p tutorial # make directory for go package
|
mkdir -p go/tutorialpb # make directory for go package
|
||||||
protoc $$PROTO_PATH --go_out=tutorial addressbook.proto
|
protoc $$PROTO_PATH --go_opt=paths=source_relative --go_out=go/tutorialpb addressbook.proto
|
||||||
@touch protoc_middleman_go
|
|
||||||
|
|
||||||
protoc_middleman_dart: addressbook.proto
|
protoc_middleman_dart: addressbook.proto
|
||||||
mkdir -p dart_tutorial # make directory for the dart package
|
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
|
list_people_dart: list_people.dart protoc_middleman_dart
|
||||||
|
|
||||||
go_mod:
|
add_person_go: go/cmd/add_person/add_person.go go/tutorialpb/addressbook.pb.go
|
||||||
go mod init github.com/protocolbuffers/protobuf/examples
|
cd go && go build -o ../add_person_go ./cmd/add_person
|
||||||
go mod tidy
|
|
||||||
|
|
||||||
add_person_go: add_person.go protoc_middleman_go go_mod
|
add_person_gotest: go/tutorialpb/addressbook.pb.go
|
||||||
go build -o add_person_go add_person.go
|
cd go && go test ./cmd/add_person
|
||||||
|
|
||||||
add_person_gotest: add_person_test.go add_person_go go_mod
|
list_people_go: go/cmd/list_people/list_people.go go/tutorialpb/addressbook.pb.go
|
||||||
go test add_person.go add_person_test.go
|
cd go && go build -o ../list_people_go ./cmd/list_people
|
||||||
|
|
||||||
list_people_go: list_people.go protoc_middleman_go go_mod
|
list_people_gotest: go/tutorialpb/addressbook.pb.go
|
||||||
go build -o list_people_go list_people.go
|
cd go && go test ./cmd/list_people
|
||||||
|
|
||||||
list_people_gotest: list_people.go list_people_go go_mod
|
|
||||||
go test list_people.go list_people_test.go
|
|
||||||
|
|
||||||
javac_middleman: AddPerson.java ListPeople.java protoc_middleman
|
javac_middleman: AddPerson.java ListPeople.java protoc_middleman
|
||||||
javac -cp $$CLASSPATH AddPerson.java ListPeople.java com/example/tutorial/AddressBookProtos.java
|
javac -cp $$CLASSPATH AddPerson.java ListPeople.java com/example/tutorial/AddressBookProtos.java
|
||||||
|
@ -91,22 +91,18 @@ scripts) and can be used to create/display an address book data file.
|
|||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
The Go example requires a plugin to the protocol buffer compiler, so it is not
|
Follow instructions in [../README.md](../README.md) to install protoc. Then
|
||||||
build with all the other examples. See:
|
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).
|
Build the Go samples with "make go". This creates the following
|
||||||
|
executable files in the current directory:
|
||||||
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:
|
|
||||||
|
|
||||||
add_person_go list_people_go
|
add_person_go list_people_go
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ option csharp_namespace = "Google.Protobuf.Examples.AddressBook";
|
|||||||
// [END csharp_declaration]
|
// [END csharp_declaration]
|
||||||
|
|
||||||
// [START go_declaration]
|
// [START go_declaration]
|
||||||
option go_package = "../tutorial";
|
option go_package = "github.com/protocolbuffers/protobuf/examples/go/tutorialpb";
|
||||||
// [END go_declaration]
|
// [END go_declaration]
|
||||||
|
|
||||||
// [START messages]
|
// [START messages]
|
||||||
|
@ -9,8 +9,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
pb "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
|
||||||
pb "github.com/protocolbuffers/protobuf/examples/tutorial"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func promptForAddress(r io.Reader) (*pb.Person, error) {
|
func promptForAddress(r io.Reader) (*pb.Person, error) {
|
@ -4,8 +4,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
pb "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
|
||||||
pb "github.com/protocolbuffers/protobuf/examples/tutorial"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPromptForAddressReturnsAddress(t *testing.T) {
|
func TestPromptForAddressReturnsAddress(t *testing.T) {
|
||||||
@ -51,7 +51,7 @@ unknown
|
|||||||
}
|
}
|
||||||
for i := 0; i < phones; i++ {
|
for i := 0; i < phones; i++ {
|
||||||
if !proto.Equal(got.Phones[i], want[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])
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -7,8 +7,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
pb "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
|
||||||
pb "github.com/protocolbuffers/protobuf/examples/tutorial"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func writePerson(w io.Writer, p *pb.Person) {
|
func writePerson(w io.Writer, p *pb.Person) {
|
@ -5,7 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
pb "github.com/protocolbuffers/protobuf/examples/tutorial"
|
pb "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWritePersonWritesPerson(t *testing.T) {
|
func TestWritePersonWritesPerson(t *testing.T) {
|
5
examples/go/go.mod
Normal file
5
examples/go/go.mod
Normal 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
6
examples/go/go.sum
Normal 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=
|
Loading…
Reference in New Issue
Block a user