dongxin1980 2019-08-25 17:30
浏览 576

如何在Go和gRPC中构建结构数组?

I am trying to add an array of strings into the content part of a struct with the Id as the array index. I have the code working for one element, but get various errors when I try and add the loop. Any ideas are welcome. I am using Go.

func buildRequest(s []string) []*storepb.LongStoreRequest { 
//      ss:= []storepb.LongStoreRequest 
// int32 i =0 stringv := s[0] 
//      for i := 0; i < len(s); i++ {
    //              println(i, apps[i])
    ss := []*storepb.LongStoreRequest{
            &storepb.LongStoreRequest {
                    Msg: &storepb.StoreMessage{
                            Content: stringv,
                            Account:  "trevor3",
                            Parent: "parentrec",
                            Id: 0,
                    },
                    }, 
            } //      }
        return ss 
}
  • 写回答

1条回答 默认 最新

  • duancilan5124 2019-08-25 17:38
    关注

    If I understand your description correctly, you want to build an array of LongStoreRequests, where each element corresponding to an item in the string array, with Id giving the array index. If that's really what you need, something like this should work:

    ss := []*storepb.LongStoreRequest{}
    for i,str:=range s {
      ss=append(ss,&storepb.LongStoreRequest {
                      Msg: &storepb.StoreMessage{
                                Content: str,
                                Account:  "trevor3",
                                Parent: "parentrec",
                                Id: i,
                        }})
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度