Link Search Menu Expand Document

Creating main.go

Before creating main.go file we are assuming that the user has created a go.mod with the name, if not please refer to Creating go.mod file. The import here is using the path to the generated files in proto/helloworld relative to the root of the repository.

package main

import (


	helloworldpb ""

type server struct{

func NewServer() *server {
	return &server{}

func (s *server) SayHello(ctx context.Context, in *helloworldpb.HelloRequest) (*helloworldpb.HelloReply, error) {
	return &helloworldpb.HelloReply{Message: in.Name + " world"}, nil

func main() {
	// Create a listener on TCP port
	lis, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatalln("Failed to listen:", err)

	// Create a gRPC server object
	s := grpc.NewServer()
	// Attach the Greeter service to the server
	helloworldpb.RegisterGreeterServer(s, &server{})
	// Serve gRPC Server
	log.Println("Serving gRPC on")

Read More

For more refer to gRPC docs