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 41 42 43 44
| package main
import ( "add_client/pb" "context" "flag" "log" "time"
"google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" )
var ( x = flag.Int("x", 10, "x的值") y = flag.Int("y", 110, "x的值") )
func main() { flag.Parse() conn, err := grpc.Dial("127.0.0.1:3000", grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { log.Fatalf("grpc.Dial failed,err:%v", err) return } defer conn.Close()
client := pb.NewAddClient(conn) ctx, cacel := context.WithTimeout(context.Background(), time.Second) defer cacel() resp, err := client.CalcService(ctx, &pb.AddRequest{ X: int64(*x), Y: int64(*y), }) if err != nil { log.Fatalf("client.CalcService failed,err:%v", err) return } log.Printf("res:%v", resp.GetRes()) }
|