https:\/\/github.com\/rinoymjoseph\/learning-go<\/a><\/p>\n\n\n\nCreate a directory<\/strong><\/p>\n\n\n\nmkdir grpc-demo<\/code><\/pre>\n\n\n\nInitialize a go project named ‘grpc-demo’<\/strong><\/p>\n\n\n\n go mod init rinoymjoseph.github.com\/grpc-demo<\/code><\/pre>\n\n\n\nCreate a directory to place proto file<\/strong><\/p>\n\n\n\nmkdir calc<\/code><\/pre>\n\n\n\nsyntax= \"proto3\";\noption go_package= \"rinoymjoseph.github.com\/grpc-demo\/calc\";\n\nservice calc {\n rpc Add(AddRequest) returns (AddResponse);\n}\n\nmessage AddRequest {\n int32 num1 = 1;\n int32 num2 = 2;\n}\n\nmessage AddResponse {\n int64 sum = 1;\n}<\/code><\/pre>\n\n\n\nInstall protoc-gen-go and protoc-gen-go-grpc. Add GOPATH\/bin to path<\/strong><\/p>\n\n\n\ngo install google.golang.org\/protobuf\/cmd\/protoc-gen-go@latest\ngo install google.golang.org\/grpc\/cmd\/protoc-gen-go-grpc@latest\nexport PATH=\"$PATH:$(go env GOPATH)\/bin\"<\/code><\/pre>\n\n\n\n Generate the gRPC stubs that can be used to implement the service and consume from clients<\/strong><\/p>\n\n\n\nprotoc --go_out=calc \\\n --go_opt=paths=source_relative \\\n --go-grpc_out=calc \\\n --go-grpc_opt=paths=source_relative \\\n calc\/calc.proto <\/code><\/pre>\n\n\n\nThis creates two files in calc folder. calc_grpc.pb.go and calc.pb.go.<\/strong><\/p>\n\n\n\nAdd the main.go file<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n\t\"context\"\n\t\"log\"\n\t\"net\"\n\n\t\"google.golang.org\/grpc\"\n\t\"rinoymjoseph.github.com\/grpc-demo\/calc\"\n)\n\ntype calcServer struct {\n\tcalc.UnimplementedCalcServer\n}\n\nfunc (s calcServer) Add(ctx context.Context, req *calc.AddRequest) (*calc.AddResponse, error) {\n\treturn &calc.AddResponse{\n\t\tSum: int64(req.Num1 + req.Num2),\n\t}, nil\n}\n\nfunc main() {\n\tlis, err := net.Listen(\"tcp\", \":8090\")\n\tif err != nil {\n\t\tlog.Fatalf(\"cannot create listener: %s\", err)\n\t}\n\tserverRegistrar := grpc.NewServer()\n\tservice := &calcServer{}\n\n\tcalc.RegisterCalcServer(serverRegistrar, service)\n\terr = serverRegistrar.Serve(lis)\n\tif err != nil {\n\t\tlog.Fatalf(\"impossible to serve: %s\", err)\n\t}\n}\n<\/code><\/pre>\n\n\n\nThe project structure<\/strong><\/p>\n\n\n\n
<\/figure><\/div>\n\n\nGet the packages<\/strong><\/p>\n\n\n\ngo get .\ngo mod tidy<\/code><\/pre>\n\n\n\nRun the gRPC server<\/strong><\/p>\n\n\n\ngo run main.go<\/code><\/pre>\n\n\n\nPostman can be used for testing the gRPC methods<\/strong><\/p>\n\n\n\n <\/figure>\n","protected":false},"excerpt":{"rendered":"TL;DR The source code is available in https:\/\/github.com\/rinoymjoseph\/learning-go Create a directory Initialize a go project named ‘grpc-demo’ Create a directory to place proto file Install protoc-gen-go and protoc-gen-go-grpc. Add GOPATH\/bin to path Generate the gRPC stubs that can be used to implement the service and consume from clients This creates two files in calc folder. … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":717,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[57,62],"_links":{"self":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/597"}],"collection":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/comments?post=597"}],"version-history":[{"count":34,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/597\/revisions"}],"predecessor-version":[{"id":718,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/597\/revisions\/718"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/media\/717"}],"wp:attachment":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/media?parent=597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/categories?post=597"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/tags?post=597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}