我正在尝试将内部类型转换为protobuf生成的类型,我无法让数组转换。我是新手,所以我不知道所有可能有用的方法。但这是我的尝试。当我运行此代码时,我得到了
panic:运行时错误:无效的内存地址或nil指针取消引用[signal SIGSEGV:segmentation violation code = 0x1 addr = 0x8 pc = 0x86c724]
以及许多其他字节数据。我想知道将内部结构转换为protobufs的最佳方法是什么。我认为我最常遇到的问题是protobuf生成的代码是指针。
原型定义
message GameHistory {
message Game {
int64 gameId = 1;
}
repeated Game matches = 1;
string username = 2;
}
message GetRequest {
string username = 1;
}
message GetGameResponse {
GameHistory gameHistory = 1;
}
去代码
// GameHistory model
type GameHistory struct {
Game []struct {
GameID int64 `json:"gameId"`
} `json:"games"`
UserName string `json:"username"`
}
func constructGameHistoryResponse(gameHistory models.GameHistory) *pb.GetGameResponse {
games := make([]*pb.GameHistory_Game, len(gameHistory.Games))
for i := range matchHistory.Matches {
games[i].GameID = gameHistory.Games[i].GameID
}
res := &pb.GetGameResponse{
GameHistory: &pb.GameHistory{
Games: games,
},
}
}