These steps cover writing and running of your application:
- From your Terminal or console application, create a new directory called ~/projects/go-programming-cookbook/chapter7/grpc, and navigate to this directory.
- Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter7/grpc
You should see a file called go.mod containing the following:
module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter7/grpc
- Copy tests from ~/projects/go-programming-cookbook-original/chapter7/grpc, or use this as an exercise to write some code of your own!
- Create a directory called greeter and navigate to it.
- Create a file called greeter.proto with the following content:
syntax = "proto3";
package greeter;
service GreeterService{
rpc Greet(GreetRequest) returns (GreetResponse) {}
}
message GreetRequest {
string greeting = 1;
string name = 2;
}
message GreetResponse{
string response = 1;
}
- Navigate back up a directory to grpc.
- Run the following command:
$ protoc --go_out=plugins=grpc:. greeter/greeter.proto
- Create a new directory called server and navigate to it.
- Create a file called greeter.go with the following content. Ensure that you modify the greeter import to use the path you set up in step 3:
package main
import (
"fmt"
"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter7/grpc/greeter"
"golang.org/x/net/context"
)
// Greeter implements the interface
// generated by protoc
type Greeter struct {
Exclaim bool
}
// Greet implements grpc Greet
func (g *Greeter) Greet(ctx context.Context, r
*greeter.GreetRequest) (*greeter.GreetResponse, error) {
msg := fmt.Sprintf("%s %s", r.GetGreeting(), r.GetName())
if g.Exclaim {
msg += "!"
} else {
msg += "."
}
return &greeter.GreetResponse{Response: msg}, nil
}
- Create a file called server.go with the following content. Ensure that you modify the greeter import to use the path you set up in step 3:
package main
import (
"fmt"
"net"
"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter7/grpc/greeter"
"google.golang.org/grpc"
)
func main() {
grpcServer := grpc.NewServer()
greeter.RegisterGreeterServiceServer(grpcServer,
&Greeter{Exclaim: true})
lis, err := net.Listen("tcp", ":4444")
if err != nil {
panic(err)
}
fmt.Println("Listening on port :4444")
grpcServer.Serve(lis)
}
- Navigate back up a directory to grpc.
- Create a new directory called client and navigate to it.
- Create a file called client.go with the following content. Ensure that you modify the greeter import to use the path you set up in step 3:
package main
import (
"context"
"fmt"
"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter7/grpc/greeter"
"google.golang.org/grpc"
)
func main() {
conn, err := grpc.Dial(":4444", grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()
client := greeter.NewGreeterServiceClient(conn)
ctx := context.Background()
req := greeter.GreetRequest{Greeting: "Hello", Name:
"Reader"}
resp, err := client.Greet(ctx, &req)
if err != nil {
panic(err)
}
fmt.Println(resp)
req.Greeting = "Goodbye"
resp, err = client.Greet(ctx, &req)
if err != nil {
panic(err)
}
fmt.Println(resp)
}
- Navigate back up a directory to grpc.
- Run go run ./server, and you will see the following output:
$ go run ./server
Listening on port :4444
- In a separate Terminal, run go run ./client from the grpc directory, and you will see the following output:
$ go run ./client
response:"Hello Reader!"
response:"Goodbye Reader!"
- The go.mod file may be updated and go.sum file should now be present in the top-level recipe directory.
- If you copied or wrote your own tests, go up one directory and run go test. Ensure that all the tests pass.