记录下protoc生成go文件后,使用grpc调用的过程
go get -u -v google.golang.org/grpc
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"net"
"rpc/student"
"strconv"
)
type Stu struct {
}
func (s Stu) GetInfo (ctx context.Context, req *student.StuReq) (*student.Student, error){
student := student.Student{
Score: 10,
P: &student.Person{
Name: "hello"+ strconv.Itoa(int(req.Id)),
Sex: 0,
},
Like: []string{
"football",
"sport",
},
}
return &student,nil
}
func main(){
grpcServer := grpc.NewServer()
student.RegisterStuServiceServer(grpcServer,new(Stu))
listen,err := net.Listen("tcp","127.0.0.1:8082")
if err != nil{
fmt.Println(err)
}
defer listen.Close()
grpcServer.Serve(listen)
}
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"rpc/student"
)
func main(){
client,err := grpc.Dial("127.0.0.1:8082",grpc.WithInsecure())
if err != nil{
fmt.Println(err)
}
defer client.Close()
stuClient := student.NewStuServiceClient(client)
stuReq := student.StuReq{Id:1}
stu,err := stuClient.GetInfo(context.TODO(),&stuReq)
if err != nil{
fmt.Println(err)
}
fmt.Println(stu)
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有