英文:
invalid memory address when creating grpc client request with repeated field
问题
我有一个关于我的grpc服务器和客户端的问题,我正在尝试在grpc中创建一个重复字段,并将其发送给客户端。但是在错误上失败了,我在我的grpc客户端/服务器上做错了什么?我尝试将RequestsCommand作为值发送,但这没有起作用。
panic: 运行时错误:无效的内存地址或空指针解引用
[信号SIGSEGV:分段违规代码=0x1 addr=0x20 pc=0x165cf0e]
goroutine 40 [运行中]:
main.(*Server).RunCommands(0x172b960?,{0x18be540?,0xc00031c030?},0x107a360?)
<autogenerated>:1 +0x2e
github.com/mygithub/some-package/proto._ExecuterService_RunCommands_Handler({0x1701a20?,0xc0002430d0},{0x18be540,0xc00031c030},0xc000318070,0x0)
..../executer_grpc.pb.go:93 +0x170
executer_grpc.pb.go:93 是:
in := new(CommandsRequest)
return srv.(ExecuterServiceServer).RunCommands(ctx, in)
在服务器端,我有以下内容:
grpc_server.go:
package main
import (
"fmt"
pb "github.com/mygithub/some-package/proto"
"google.golang.org/grpc"
)
type Server struct {
pb.ExecuterServiceServer
}
func startServer() {
addr := GetAgentAddress()
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("Faild to listen on %v\n", err)
}
s := grpc.NewServer()
pb.RegisterExecuterServiceServer(s, &Server{})
if err = s.Serve(lis); err != nil {
log.Fatalf("Failed to serve: %v\n", err)
}
}
grpc_client.go:
package main
import (
"context";
"encoding/json"
"fmt"
v1 "github.com/mygithub/some-package/api/v1"
pb "github.com/mygithub/some-package/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func sendCommandsWithClient() (string, error) {
command := v1.CommandSpec{
BashCommand: "ls",
Async: false,
}
addr := "localhost:4000"
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return "", err
}
defer conn.Close()
client := pb.NewExecuterServiceClient(conn)
if err != nil {
return "", err
}
commandRequest := &pb.CommandRequest{
BashCommand: command.BashCommand,
Slug: "test",
Async: command.Async,
}
req := &pb.CommandsRequest{}
req.Commands = append(req.Commands, commandRequest)
aAsJsonString, _ := json.Marshal(req)
fmt.Println(string(aAsJsonString))
res, err := client.RunCommands(context.Background(), req)
if err != nil {
return "", err
}
return res.Content, nil
}
func main() {
_, err := sendCommandsWithClient()
if err != nil {
return
}
}
RunCommands:
func (s *Server) RunCommands(ctx context.Context, in *pb.CommandsRequest) (*pb.CommandsResponse, error) {
log.Println("test")
command := v1.CommandSpec{BashCommand: "ls", Slug: "test", Async: true}
CommandsQueue := append(CommandsQueue, command)
size := len(CommandsQueue)
return &pb.CommandsResponse{Status: 0, Content: strconv.Itoa(size)}, nil
}
go版本:go version go1.18.2 darwin/amd64
protoc:proto3
英文:
I have an issue with my grpc server and client, I'm trying to create a repeated field in the grpc and send it to the client. but it failed on error, what am I doing wrong on my grpc client/server? i tried send the RequestsCommand as value but this didn't work to.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x165cf0e]
goroutine 40 [running]:
main.(*Server).RunCommands(0x172b960?, {0x18be540?, 0xc00031c030?}, 0x107a360?)
<autogenerated>:1 +0x2e
github.com/mygithub/some-package/proto._ExecuterService_RunCommands_Handler({0x1701a20?, 0xc0002430d0}, {0x18be540, 0xc00031c030}, 0xc000318070, 0x0)
..../executer_grpc.pb.go:93 +0x170
executer_grpc.pb.go:93 is :
in := new(CommandsRequest)
return srv.(ExecuterServiceServer).RunCommands(ctx, in)
on the server side I have the following:
grpc_server.go:
package main
import (
"fmt"
pb "github.com/mygithub/some-package/proto"
"google.golang.org/grpc"
)
type Server struct {
pb.ExecuterServiceServer
}
func startServer() {
addr := GetAgentAddress()
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("Faild to listen on %v\n", err)
}
s := grpc.NewServer()
pb.RegisterExecuterServiceServer(s, &Server{})
if err = s.Serve(lis); err != nil {
log.Fatalf("Failed to serve: %v\n", err)
}
}
grpc_client.go:
package main
import (
"context"
"encoding/json"
"fmt"
v1 "github.com/mygithub/some-package/api/v1"
pb "github.com/mygithub/some-package/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func sendCommandsWithClient() (string, error) {
command := v1.CommandSpec{
BashCommand: "ls",
Async: false,
}
addr := "localhost:4000"
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return "", err
}
defer conn.Close()
client := pb.NewExecuterServiceClient(conn)
if err != nil {
return "", err
}
commandRequest := &pb.CommandRequest{
BashCommand: command.BashCommand,
Slug: "test",
Async: command.Async,
}
req := &pb.CommandsRequest{}
req.Commands = append(req.Commands, commandRequest)
aAsJsonString, _ := json.Marshal(req)
fmt.Println(string(aAsJsonString))
res, err := client.RunCommands(context.Background(), req)
if err != nil {
return "", err
}
return res.Content, nil
}
func main() {
_, err := sendCommandsWithClient()
if err != nil {
return
}
}
RunCommands:
func (s *Server) RunCommands(ctx context.Context, in *pb.CommandsRequest) (*pb.CommandsResponse, error) {
log.Println("test")
command := v1.CommandSpec{BashCommand: "ls", Slug: "test", Async: true}
CommandsQueue := append(CommandsQueue, command)
size := len(CommandsQueue)
return &pb.CommandsResponse{Status: 0, Content: strconv.Itoa(size)}, nil
}
go version: go version go1.18.2 darwin/amd64
protoc: proto3
答案1
得分: 0
你应该使用"go build"或"go run"命令来编译或运行你的所有Go文件。
go run main.go model.go somefile.go
英文:
you should go build or go run with all of your go files.
go run main.go model.go somefile.go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论