1. 安装和配置
linux
# fetches this repo into $GOPATHgo get -d github.com/envoyproxy/protoc-gen-validate# installs PGV into $GOPATH/bingo build
windows
将 zip文件中的exe文件拷贝到 go的根补录的bin目录下
生成python源码
protoc -I . --go_out=plugins=grpc:. --validate_out="lang=go:." helloworld.proto
2. proto
- 新建validate.proto文件内容从 https://github.com/envoyproxy/protoc-gen-validate/blob/master/validate/validate.proto 拷贝
 - 新建helloworl.proto文件 ```protobuf syntax = “proto3”;
 
import “validate.proto”; option go_package=”.;proto”;
service Greeter { rpc SayHello (Person) returns (Person); }
message Person { uint64 id = 1 [(validate.rules).uint64.gt = 999];
string email = 2 [(validate.rules).string.email = true]; string name = 3 [(validate.rules).string = { pattern: “^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$”,max_bytes: 256,}];
}
<a name="zDJmx"></a>## 3. 服务端(server)```gopackage mainimport ("context""google.golang.org/grpc/codes""google.golang.org/grpc/status""net""google.golang.org/grpc""start/pgv_test/proto")type Server struct{}func (s *Server) SayHello(ctx context.Context, request *proto.Person) (*proto.Person,error){return &proto.Person{Id: 32,}, nil}type Validator interface {Validate() error}func main(){var interceptor grpc.UnaryServerInterceptorinterceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {// 继续处理请求if r, ok := req.(Validator); ok {if err := r.Validate(); err != nil {return nil, status.Error(codes.InvalidArgument, err.Error())}}return handler(ctx, req)}var opts []grpc.ServerOptionopts = append(opts, grpc.UnaryInterceptor(interceptor))g := grpc.NewServer(opts...)proto.RegisterGreeterServer(g, &Server{})lis, err := net.Listen("tcp", "0.0.0.0:50051")if err != nil{panic("failed to listen:"+err.Error())}err = g.Serve(lis)if err != nil{panic("failed to start grpc:"+err.Error())}}
4. 客户端(client)
package mainimport ("context""fmt""google.golang.org/grpc""start/pgv_test/proto")type customCredential struct{}func main() {var opts []grpc.DialOption//opts = append(opts, grpc.WithUnaryInterceptor(interceptor))opts = append(opts, grpc.WithInsecure())conn, err := grpc.Dial("localhost:50051", opts...)if err != nil {panic(err)}defer conn.Close()c := proto.NewGreeterClient(conn)//rsp, _ := c.Search(context.Background(), &empty.Empty{})rsp, err := c.SayHello(context.Background(), &proto.Person{Email: "bobby",})if err != nil {panic(err)}fmt.Println(rsp.Id)}
