/
grpc-client.go
40 lines (33 loc) · 987 Bytes
/
grpc-client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//++++++++++++++++++++++++++++++++++++++++
// 《Go Web编程实战派从入门到精通》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goWebActualCombat
// 仓库地址:https://github.com/shirdonl/goWebActualCombat
//++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
pb "gitee.com/shirdonl/goWebActualCombat/chapter5/protobuf"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
)
func main() {
conn, err := grpc.Dial(":8078", grpc.WithInsecure())
if err != nil {
log.Fatalf("dial error: %v\n", err)
}
defer conn.Close()
// 实例化 ProgrammerService
client := pb.NewProgrammerServiceClient(conn)
// 调用服务
req := new(pb.Request)
req.Name = "shirdon"
resp, err := client.GetProgrammerInfo(context.Background(), req)
if err != nil {
log.Fatalf("resp error: %v\n", err)
}
fmt.Printf("Recevied: %v\n", resp)
}