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

我有一个动态多维数组,我需要从循环中动态填充。我如何定义数组并填充数据。


这是我正在尝试的代码


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++

}

我面临的错误:


non-integer slice index "OrderNumber"

non-integer slice index "customernum"

non-integer slice index "orderstatus"

non-integer slice index "orderprice"

我在 php 中做了同样的事情并且工作完美:


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";

}

我是 golang 的新手,无法找到问题出在哪里,非常感谢任何帮助。


明月笑刀无情
浏览 150回答 4
4回答

波斯汪

让我们考虑这个解决方案:arrDetails := map[int]map[string]string{}index := 0for _, 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++}要将结果转换为 json(正如我看到你在对@liao yu 的回答的评论中提出的问题),我们应该学习更多关于标签的知识: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))}

天涯尽头无女友

你可以试试这个:import "fmt"func main() {&nbsp; &nbsp; var arrDetails []map[string]string&nbsp; &nbsp; var index int = 0&nbsp; &nbsp; //for _, orderdetails := range ordersfromdb {&nbsp; &nbsp; for i:=0; i<5;i++ {&nbsp; &nbsp; &nbsp; &nbsp; detail := make(map[string]string)&nbsp; &nbsp; &nbsp; &nbsp; detail["OrderNumber"] = "001"&nbsp; &nbsp; &nbsp; &nbsp; detail["customernum"] = "cust_001"&nbsp; &nbsp; &nbsp; &nbsp; detail["orderstatus"] = "open"&nbsp; &nbsp; &nbsp; &nbsp; detail["orderprice"] = "200"&nbsp; &nbsp; &nbsp; &nbsp; arrDetails = append(arrDetails, detail)&nbsp; &nbsp; &nbsp; &nbsp; index++&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("Hello, playground %+v", arrDetails )}

慕的地10843

正如您在此处将arrDetails变量定义为多维切片一样[][]string。这意味着您不能将 a 分配string给它keys,而可以将字符串分配为值。你可以像下面我提到的那样做你的代码。package mainimport (&nbsp; &nbsp; "fmt")func main() {&nbsp; var arrDetails [][]string&nbsp; var s []string&nbsp; var index int&nbsp; for i:=0; i<5;i++ {&nbsp; &nbsp; &nbsp;s = []string{"001", "cust_001", "open", "200"}&nbsp; &nbsp; &nbsp;arrDetails = append(arrDetails, s)&nbsp; &nbsp; &nbsp;index++&nbsp; }&nbsp;fmt.Printf("Hello, playground %+v", arrDetails )}或者,如果你想要keys配对value,那么你必须用作map:var arrDetails map[string]string

www说

我按如下方式填充多维数组arrDetails[index][0] = "001"arrDetails[index][1] = "cust_001"arrDetails[index][2] = "open"arrDetails[index][3] = "200"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go