The error message “cannot use UserServer{} (value of type UserServer) as user_service.UserServiceServer value in assignment: UserServer does not implement user_service.UserServiceServer (method GetUserInfo has pointer receiver)” indicates that the UserServer type doesn't fulfill the user_service.UserServiceServer interface due to the GetUserInfo method having a pointer receiver.

To resolve this, modify the receiver type of the GetUserInfo method to a pointer type. Here's the corrected code:

func main() {
	DB.ConnectDb()
	// 监听端口
	list, err := net.Listen("tcp", "127.0.0.1:2346")
	if err != nil {
		panic(err)
	}
	fmt.Println("grpc listening on 2346 port")
	server := grpc.NewServer()

	var w user_service.UserServiceServer
	w = &UserServer{} // Use pointer to UserServer

	user_service.RegisterUserServiceServer(server, w)
	if err := server.Serve(list); err != nil {
		panic(err)
	}
}

type UserServer struct{}

func (u *UserServer) GetUserInfo(ctx context.Context, request *user_service.Request) (*user_service.User, error) {
	// Implementation of GetUserInfo method
}

By changing w = UserServer{} to w = &UserServer{}, we ensure that UserServer is utilized as a pointer type, satisfying the user_service.UserServiceServer interface.

Go gRPC: Fix “cannot use ... as ... value in assignment” Error with Pointer Receiver

原文地址: https://www.cveoy.top/t/topic/qfKi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录