dongyilu3143 2018-10-16 08:54
浏览 413
已采纳

如何在golang中将数据插入多维数组?

I have a dynamic multi dimension array, I need to fill dynamically from the loop. how can i define the array and fill the data.

Here is the code which im trying

var arrDetails[][]string
var index int = 0
for _, orderdetails := range ordersfromdb {
    arrDetails[index]["OrderNumber"] = "001"
    arrDetails[index]["customernum"] = "cust_001"
    arrDetails[index]["orderstatus"] = "open"
    arrDetails[index]["orderprice"] = "200"
    index++
}

error which im facing:

non-integer slice index "OrderNumber"
non-integer slice index "customernum"
non-integer slice index "orderstatus"
non-integer slice index "orderprice"

I have done the same in php and works perfect:

for ($i=0;$i<5:$i++)
{
     $arr_orderdetails[$i]["OrderNumber"] = "001";
     $arr_orderdetails[$i]["customernum"] = "cust_001";
     $arr_orderdetails[$i]["orderstatus"] = "open";
     $arr_orderdetails[$i]["orderprice"] = "200";
}

I'm new to golang, not able to find where it is going wrong, any help much appreciated.

Thanks :)

  • 写回答

4条回答 默认 最新

  • doujiao6116 2018-10-16 12:22
    关注

    Let's consider this solution:

    arrDetails := map[int]map[string]string{}
    
    index := 0
    for _, orderdetails := range ordersfromdb {
        arrDetails[index] = map[string]string{} // you have to initialize map
    
        arrDetails[index]["OrderNumber"] = "001"
        arrDetails[index]["customernum"] = "cust_001"
        arrDetails[index]["orderstatus"] = "open"
        arrDetails[index]["orderprice"] = "200"
    
        index++
    }
    

    To convert results to json (as I saw you question in comment to @liao yu's answer), we should learn something more about tags:

    import (
        "encoding/json"
        "fmt"
    )
    
    type OrderDetails struct {
        Number   string `json:"number"`
        Customer string `json:"customer"`
        Status   string `json:"status"`
        Price    string `json:"price"`
    }
    
    func main() {
        ordersfromdb := []int{1, 2, 3}
    
        var arrDetails []OrderDetails
        for _, v := range ordersfromdb {
            arrDetails = append(arrDetails, OrderDetails{
                Number:   fmt.Sprintf("order_number_%v", v),
                Customer: fmt.Sprintf("customer_%v", v),
                Status:   fmt.Sprintf("order_status_%v", v),
                Price:    fmt.Sprintf("$%v", v),
            })
        }
    
        data, err := json.Marshal(arrDetails)
        if err != nil {
            panic(err)
        }
        fmt.Println(string(data))
    }
    

    See it on playground: https://play.golang.org/p/IA0G53YX_dZ

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置