Hello World
// - proto/example.proto -
syntax = "proto3";
option go_package = "proto;helloworld_proto";
package ua.made.helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello(HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest { string name = 1; }
// The response message containing the greetings
message HelloReply { string message = 1; }
// - main.go -
// Package main implements a server for Greeter service.
package main
import (
"context"
"fmt"
"log"
"net"
pb "github.com/butuzov/sandbox/grpc/helloworld/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
func main() {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
fmt.Println("listening on", listener.Addr().String())
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
reflection.Register(s)
log.Fatalf("failed to serve: %v", s.Serve(listener))
}