package main import ( "fmt" ) type student struct { Name string Age int } funcpase_student()map[string]*student { m := make(map[string]*student) stus := []student{ {Name: "zhou", Age: 24}, {Name: "li", Age: 23}, {Name: "wang", Age: 22}, } for _, stu := range stus { m[stu.Name] = &stu } return m } funcmain() { students := pase_student() for k, v := range students { fmt.Printf("key=%s,value=%v \n", k, v) } }
package main import ( "fmt" ) type student struct { Name string Age int } funcpase_student()map[string]*student { m := make(map[string]*student) stus := []student{ {Name: "zhou", Age: 24}, {Name: "li", Age: 23}, {Name: "wang", Age: 22}, } for _, stu := range stus { m[stu.Name] = &stu } return m } funcmain() { students := pase_student() for k, v := range students { fmt.Printf("key=%s,value=%v \n", k, v) } }