golang-gRPC (Mac)

abc101
3 min readJun 18, 2021

Hello World Protocol Buffer

Preparing

> brew update && brew install protobuf

Create a go project(ex: lets-learn-golang-grpc)

> mkdir lets-learn-golang-grpc
> cd lets-learn-golang-grpc
llgg > go mod init lets-learn-golang-grpc
llgg > go get -u google.golang.org/grpc
llgg > go get -u google.golang.org/protobuf/proto
llgg > go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc

The go modules will be used whole the project.

Create a `chapter-00` for `helloworld` such as,

├── chapter-00
│ └── helloworld
│ ├── README.md
│ ├── client
│ │ └── main.go
│ ├── pb
│ │ ├── helloworld.proto
│ └── server
│ └── main.go
├── go.mod
└── go.sum

Create `pb/helloworld.proto`

syntax = "proto3";

option go_package = "lets-learn-golang-grpc/chapter-00/helloworld/pb";

package pb;

// The service definition
service HelloWorld {
// Sends the SayHello message
rpc SayHello (HelloRequest) returns (HelloResponse) {}
}

// The request message containing a name(Client-side)
message HelloRequest {
string name = 1;
}

// The response message containing a message(Server-side)
message HelloResponse {
string message = 1;
}

The name will receive from the client (user) and send a message to the client .

Generate go codes with `pb/helloworld.proto`

chapter-00/helloworld > protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative pb/helloworld.proto

This will create two go codes, helloworld.pb.go and helloworld_grpc.pb.go.

├── pb
├── helloworld.pb.go
├── helloworld.proto
└── helloworld_grpc.pb.go

Create a server `server/main.go`

package main

import (
"context"
"lets-learn-golang-grpc/chapter-00/helloworld/pb"
"log"
"net"

"google.golang.org/grpc"
)

const (
port = ":50051"
)

type server struct {
pb.UnimplementedHelloWorldServer
}

// SayHello implements a server
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloResponse{Message: "Hello " + in.GetName()}, nil
}

func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterHelloWorldServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}

This server will return a message “Hello ” and a name that user send.

Create a client `client/main.go`

package main

import (
"context"
"lets-learn-golang-grpc/chapter-00/helloworld/pb"
"log"
"os"
"time"

"google.golang.org/grpc"
)

const (
address = "localhost:50051"
defaultName = "World"
)

func main() {
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()

c := pb.NewHelloWorldClient(conn)

// get a name otherwise defaultName
name := defaultName
if (len(os.Args)) > 1 {
name = os.Args[1]
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not say: %v", err)
}
log.Printf("Say: %s", r.GetMessage())
}

This server will send a name to server and receive a message from the server. The default name is World .

Directory tree

.
├── chapter-00
│ └── helloworld
│ ├── README.md
│ ├── client
│ │ └── main.go
│ ├── pb
│ │ ├── helloworld.pb.go
│ │ ├── helloworld.proto
│ │ └── helloworld_grpc.pb.go
│ └── server
│ └── main.go
├── go.mod
└── go.sum

Run the server in the root of project or the server directory

> go run ./chapter-00/server/main.go

Run the client in the root of project or the client directory

> go run ./chapter-00/client/main.go

The result is

> Say: Hello World

When you put a name such as

> go run ./chapter-00/server/main.go Alice

you will have

> Say: Hello Alice

This is a very simple golang-gRPC example.

https://github.com/abc101/lets-learn-golang-grpc

--

--