go - 将内部 go struct 数组转换为 protobuf 生成的指针数组
问题描述
我正在尝试将内部类型转换为 protobuf 生成的类型,但无法获取要转换的数组。我是新手,所以我不知道所有可以提供帮助的方法。但这是我的尝试。运行此代码时,我得到
恐慌:运行时错误:无效的内存地址或 nil 指针取消引用 [信号 SIGSEGV:分段违规代码 = 0x1 地址 = 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,
},
}
}
解决方案
推荐阅读
- sql - SQL Select语句从多个表中填充datagridview
- javascript - 如何在我的多步表单中验证我的输入类型收音机?当我单击下一步时,它会进入下一步
- python - 我可以使用函数在python中导入模块吗
- excel - 使用 VBA 添加 activex 控件组合框
- python - 在线程中是否有任何方法可以将一个类的函数调用到另一个类?
- regex - 格式 MMAYY 的正则表达式正则表达式 - A 是任何字母,MM 作为月份,YY 作为年份
- firebase - 将集合和子集合数据添加到来自 Firestore Firebase 的模型实体
- python - 如何将值传递给公式?
- kentico - Kentico 更新媒体库到 Azure 的直接路径
- java - 不使用杰克逊 REST Web 服务(RESTful Spring 控制器)@PathVariable 处理请求