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 excel 上下按钮 显示行
  • ¥20 搭建三相栅极电路后高侧浮动地VS存在电容特性
  • ¥20 云卓h12pro 数传问题
  • ¥20 请问有人知道怎么用工艺库里面的sdb文件通过virtuoso导出来library里面每个cell的symbol吗?
  • ¥20 海思 nnie 编译 报错
  • ¥50 决策面并仿真,要求有仿真结果图
  • ¥15 关于路由器的路由协议配置
  • ¥15 springboot接入微信支付SDK
  • ¥50 大区域的遥感影像匹配 怎么做啊
  • ¥15 求解答:pytorch跑yolov8神经网络受挫