字符串类型

后端:

  1. func Hello(ctx *gin.Context){
  2. name := "joker"
  3. ctx.HTML(http.StatusOK,"index.html",name)
  4. }

前端:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <h1>{{.}}</h1>
  9. </body>
  10. </html>

结构体类型

后端:

  1. type User struct {
  2. Id int
  3. Name string
  4. }
  5. func Hello(ctx *gin.Context){
  6. user := User{
  7. Id: 1,
  8. Name: "joker",
  9. }
  10. ctx.HTML(http.StatusOK,"index.html",user)
  11. }

前端:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <img src="/static/images/1.jpg">
  9. <h1>你好</h1>
  10. {{.Id}}
  11. {{.Name}}
  12. </body>
  13. </html>

数组类型

后端:

  1. func Hello(ctx *gin.Context){
  2. var users = [2]string{
  3. "joker",
  4. "jack",
  5. }
  6. ctx.HTML(http.StatusOK,"index.html",users)
  7. }

前端:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. {{/*第一种循环方式*/}}
  9. {{ range . }}
  10. {{.}}
  11. {{end}}
  12. <br>
  13. {{/*第二种循环方式*/}}
  14. {{range $i,$v := .}}
  15. {{$i}}
  16. {{$v}}
  17. {{end}}
  18. </body>
  19. </html>

结构体数组类型

后端:

  1. type User struct {
  2. Id int
  3. Name string
  4. }
  5. func Hello(ctx *gin.Context){
  6. var users = [2]User{
  7. {
  8. Id: 1,
  9. Name: "joker",
  10. },
  11. {
  12. Id: 2,
  13. Name: "jack",
  14. },
  15. }
  16. ctx.HTML(http.StatusOK,"index.html",users)
  17. }

前端:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. {{/*第一种循环方式*/}}
  9. {{ range . }}
  10. {{.Id}}<br>
  11. {{.Name}}
  12. <hr>
  13. {{end}}
  14. <br>
  15. {{/*第二种循环方式*/}}
  16. {{ range $i,$v := . }}
  17. {{$v.Id}}<br>
  18. {{$v.Name}}
  19. <hr>
  20. {{end}}
  21. <br>
  22. </body>
  23. </html>

Map类型

后端:

  1. func Hello(ctx *gin.Context){
  2. var users = map[string]string{
  3. "name":"joker",
  4. "age":"30",
  5. }
  6. ctx.HTML(http.StatusOK,"index.html",users)
  7. }

前端:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. {{.name}}
  9. {{.age}}
  10. <br>
  11. </body>
  12. </html>

Map数组类型

后端:

  1. func Hello(ctx *gin.Context){
  2. var users = [2]map[string]string{
  3. {
  4. "name": "joker",
  5. "age": "30",
  6. },
  7. {
  8. "name": "jack",
  9. "age": "40",
  10. },
  11. }
  12. ctx.HTML(http.StatusOK,"index.html",users)
  13. }

前端:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. {{range .}}
  9. {{.name}}
  10. {{.age}}
  11. <br>
  12. {{end}}
  13. <br>
  14. </body>
  15. </html>

Map结构体类型

后端:

  1. type User struct {
  2. Id int
  3. Name string
  4. }
  5. func Hello(ctx *gin.Context){
  6. var users = map[string]User{
  7. "group1": {
  8. Id: 1,
  9. Name: "joker",
  10. },
  11. "group2": {
  12. Id: 2,
  13. Name: "jack",
  14. },
  15. }
  16. ctx.HTML(http.StatusOK,"index.html",users)
  17. }

前端:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. {{.group1.Id}}
  9. {{.group1.Name}}
  10. <br>
  11. {{.group2.Id}}
  12. {{.group2.Name}}
  13. </body>
  14. </html>