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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退