Skip to content

Commit b28be15

Browse files
committed
Merge branch 'master'
2 parents 6c905d9 + fc35ec2 commit b28be15

File tree

9 files changed

+448
-0
lines changed

9 files changed

+448
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# grpc-go
2+
3+
grpc-go is a tutorial repository, demonstrating how to create a gRPC server and client in Go. The publication for this repository can be found [here](https://pascalallen.medium.com/how-to-build-a-grpc-server-in-go-943f337c4e05).
4+
5+
## Prerequisites
6+
7+
- [Go](https://go.dev/dl/)
8+
9+
## gRPC server and client usage
10+
11+
### Run the project's gRPC server from project root
12+
13+
```bash
14+
go run server/main.go
15+
```
16+
17+
### Execute the project's gRPC client request from project root
18+
19+
```bash
20+
go run client/main.go
21+
```

client/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"context"
5+
pb "github.com/pascalallen/grpc-go/helloworld"
6+
"google.golang.org/grpc"
7+
"google.golang.org/grpc/credentials/insecure"
8+
"log"
9+
"time"
10+
)
11+
12+
func main() {
13+
conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
14+
if err != nil {
15+
log.Fatalf("failed to connect to gRPC server at localhost:50051: %v", err)
16+
}
17+
defer conn.Close()
18+
c := pb.NewHelloWorldServiceClient(conn)
19+
20+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
21+
defer cancel()
22+
23+
r, err := c.SayHello(ctx, &pb.HelloWorldRequest{})
24+
if err != nil {
25+
log.Fatalf("error calling function SayHello: %v", err)
26+
}
27+
28+
log.Printf("Response from gRPC server's SayHello function: %s", r.GetMessage())
29+
}

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/pascalallen/grpc-go
2+
3+
go 1.21.1
4+
5+
require (
6+
google.golang.org/grpc v1.58.2
7+
google.golang.org/protobuf v1.31.0
8+
)
9+
10+
require (
11+
github.com/golang/protobuf v1.5.3 // indirect
12+
golang.org/x/net v0.12.0 // indirect
13+
golang.org/x/sys v0.10.0 // indirect
14+
golang.org/x/text v0.11.0 // indirect
15+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
16+
)

go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
2+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
3+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
4+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
6+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
7+
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
8+
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
9+
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
10+
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11+
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
12+
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
13+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
14+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 h1:bVf09lpb+OJbByTj913DRJioFFAjf/ZGxEz7MajTp2U=
15+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM=
16+
google.golang.org/grpc v1.58.2 h1:SXUpjxeVF3FKrTYQI4f4KvbGD5u2xccdYdurwowix5I=
17+
google.golang.org/grpc v1.58.2/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0=
18+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
19+
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
20+
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
21+
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

helloworld/helloworld.pb.go

Lines changed: 205 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helloworld/helloworld.proto

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
syntax = "proto3";
2+
3+
option go_package = "github.com/pascalallen/grpc-go/helloworld";
4+
5+
package helloworld;
6+
7+
service HelloWorldService {
8+
rpc SayHello(HelloWorldRequest) returns (HelloWorldResponse) {}
9+
}
10+
11+
message HelloWorldRequest {}
12+
13+
message HelloWorldResponse {
14+
string message = 1;
15+
}

0 commit comments

Comments
 (0)