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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵