定义及初始化
import "fmt"
// map与结构体组合使用
type User struct {
Id int
Name string
}
func main() {
map_s1 := map[string]User{"user1":{1,"加藤惠"},
"user2":{2,"雪乃"}}
fmt.Println(map_s1) #map[user1:{1 加藤惠} user2:{2 雪乃}]
}
渲染数据
controller
package teacher_controller
import "github.com/astaxie/beego"
// 自定义controller结构体
type TeacherController struct {
beego.Controller
}
// 定义一个teacher 结构体
type Teacher struct {
Name string
Gender string
}
func (this *TeacherController) Get() {
this.TplName="teacher.html"
teacher :=map[string]string {"姓名":"yjs","年龄":"28"}
map_val :=map[string]Teacher{"tea1":{"lw","male"},"tea2":{"lz","fmale"}}
this.Data["map_val"]=map_val
this.Data["teacher"]=teacher
}
router
import (
"beego_project/controllers"
"github.com/astaxie/beego"
"beego_project/controllers/teacher"
)
func init() {
beego.Router("/teacher", &teacher_controller.TeacherController{})
}
html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>teacher</title>
</head>
<body>
<h1>这是teacher页面</h1>
{{/*取出map_struct */}}
{{range .map_val}}
{{.}}
{{end}}
<br>
{{/*取出map_struct 结构体中的值 */}}
{{range .map_val}}
{{.Name}}:{{.Gender}}
{{end}}
</body>
</html>
输出