Last active
April 9, 2022 12:43
-
-
Save FerdinaKusumah/dbd2c8f01ca010d4f4f2ef52a9774211 to your computer and use it in GitHub Desktop.
grpc main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
coreProto "golang/simple-grpc/proto" | |
"google.golang.org/grpc" | |
"log" | |
"net" | |
) | |
// HelloService ... | |
type HelloService struct { | |
coreProto.UnimplementedHelloServiceServer | |
} | |
// SayHello this will be implementation method hello world | |
func (s *HelloService) SayHello(ctx context.Context, in *coreProto.HelloRequest) (*coreProto.HelloResponse, error) { | |
return &coreProto.HelloResponse{ | |
Message: "Hello From the Server !", | |
Name: in.Name, | |
}, nil | |
} | |
func main() { | |
/** | |
go get google.golang.org/protobuf/runtime/[email protected] | |
go get google.golang.org/protobuf/reflect/[email protected] | |
Generate proto enter folder go-simple-grpc then execute this command | |
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative ./proto/*.proto | |
*/ | |
serverAddress := "9090" | |
srv, err := net.Listen("tcp", fmt.Sprintf(`:%s`, serverAddress)) | |
if err != nil { | |
log.Fatalf("failed to listen: %v", err) | |
} | |
grpcServer := grpc.NewServer() | |
coreProto.RegisterHelloServiceServer(grpcServer, new(HelloService)) | |
fmt.Println(fmt.Sprintf(`Server running in address : %s`, serverAddress)) | |
if err = grpcServer.Serve(srv); err != nil { | |
log.Fatalf("failed to serve: %s", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment