我正在嘗試創建一片地圖。雖然代碼編譯好,我得到以下運行時錯誤:運行時錯誤:「在無位圖中進行賦值」
mapassign1: runtime·panicstring("assignment to entry in nil map");
我試圖做地圖的數組,其中包含兩個indicies,一個「身份證」和「投資者」每個地圖。我的代碼如下所示:
for _, row := range rows {
var inv_ids []string
var inv_names []string
//create arrays of data from MySQLs GROUP_CONCAT function
inv_ids = strings.Split(row.Str(10), ",")
inv_names = strings.Split(row.Str(11), ",")
length := len(inv_ids);
invs := make([]map[string]string, length)
//build map of ids => names
for i := 0; i < length; i++ {
invs[i] = make(map[string]string)
invs[i]["Id"] = inv_ids[i]
invs[i]["Investor"] = inv_names[i]
}//for
//build Message and return
msg := InfoMessage{row.Int(0), row.Int(1), row.Str(2), row.Int(3), row.Str(4), row.Float(5), row.Float(6), row.Str(7), row.Str(8), row.Int(9), invs}
return(msg)
} //for
我最初認爲類似下面會的工作,但它還是不解決問題。有任何想法嗎?
invs := make([]make(map[string]string), length)
您的代碼工作正常,我:HTTP://播放.golang.org/p/DaY5qdBGpy – 2013-02-27 17:22:38
如果您的地圖定義得非常好,但您可能希望使用結構。 struct {Id,Investor string} – 2013-02-27 17:24:47