您正在试图建立一个 切片 地图; 考虑以下示例:
http://play.golang.org/p/gChfTgtmN-
package mainimport "fmt"func main() { a := make([]map[string]int, 100) for i := 0; i < 100; i++ { a[i] = map[string]int{"id": i, "investor": i} } fmt.Println(a)}您可以重写这些行:
invs[i] = make(map[string]string)invs[i]["Id"] = inv_ids[i]invs[i]["Investor"] = inv_names[i]
如:
invs[i] = map[string]string{"Id": inv_ids[i], "Investor": inv_names[i]}这称为 复合文字 。
现在,在一个更加惯用的程序中,您很可能想使用a
struct代表投资者:
http://play.golang.org/p/vppK6y-c8g
package mainimport ( "fmt" "strconv")type Investor struct { Id int Name string}func main() { a := make([]Investor, 100) for i := 0; i < 100; i++ { a[i] = Investor{Id: i, Name: "John" + strconv.Itoa(i)} fmt.Printf("%#vn", a[i]) }}


