```go
package main
import (
"StudyGin/helloWorld"
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"net"
)
type HelloService struct {
helloWorld.UnimplementedHelloServer
}
func (h *HelloService) SayHello(ctx context.Context, request *helloWorld.HelloRequest) (*helloWorld.HelloResponse,error){
// 或者rsp := new(helloWorld.HelloResponse)
var rsp helloWorld.HelloResponse
rsp.Message= fmt.Sprintf("Hello %s",request.Name)
return &rsp,nil
}
func main() {
//实例化grpc Server
s:=grpc.NewServer()
//注册HelloService
var helloService HelloService
helloWorld.RegisterHelloServer(s,&helloService)
// 启动
lis, err := net.Listen("tcp", "0.0.0.0:8080")
if err != nil {
grpclog.Fatalf("Failed to listen: %v", err)
}
err = s.Serve(lis)
if err != nil {
return
}
}
```
**在学习grpc的时候,看教程服务端定义结构体不需要继承helloWorld.UnimplementedHelloServer,为什么现在需要 ?
Go版本1.17**
有疑问加站长微信联系(非本文作者)