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

如何在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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dpglo66848 2018-10-16 09:37

    As you defined here your arrDetails variable as multidimentional slice as [][]string.It means you can not assign a string into its keys while you can assign string as a value.

    You can do your code like below I mentioned.

    package main
    
    import (
        "fmt"
    )
    
    func main() {
      var arrDetails [][]string
      var s []string
      var index int
      for i:=0; i<5;i++ {
         s = []string{"001", "cust_001", "open", "200"}
         arrDetails = append(arrDetails, s)
         index++
      } 
    fmt.Printf("Hello, playground %+v", arrDetails )
    }
    

    Or if you want keys and value pair then you've to use map as:

    var arrDetails map[string]string
    
    评论
    解决 无用
    打赏 举报
  • drbmhd9583 2018-10-16 10:27

    As per volker suggestion in the comment, I'm fill the multi dimensional array as below

    arrDetails[index][0] = "001"
    arrDetails[index][1] = "cust_001"
    arrDetails[index][2] = "open"
    arrDetails[index][3] = "200"
    
    评论
    解决 无用
    打赏 举报
  • dtg25862 2018-10-16 10:58

    you can try this:

    import "fmt"
    
    func main() {
    
        var arrDetails []map[string]string
        var index int = 0
    
        //for _, orderdetails := range ordersfromdb {
        for i:=0; i<5;i++ {
            detail := make(map[string]string)
    
            detail["OrderNumber"] = "001"
            detail["customernum"] = "cust_001"
            detail["orderstatus"] = "open"
            detail["orderprice"] = "200"
            arrDetails = append(arrDetails, detail)
    
            index++
        }
        fmt.Printf("Hello, playground %+v", arrDetails )
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题