• gin架构下实现页面的数据调用


    1. package main
    2. import (
    3. "github.com/gin-gonic/gin"
    4. _ "net/http"
    5. "testgin01/myfunc"
    6. )
    7. func main() {
    8. r := gin.Default()
    9. //r.LoadHTMLFiles("/temp/hello01.html")
    10. r.LoadHTMLGlob("temp/**/*") // **/代表一个文件级
    11. //指定cSS文件
    12. r.Static("/s","style") //css文件需要使用相对路径
    13. //r.StaticFS("/s",http.Dir("static")) 另外一种引入CSS路径的映射方式
    14. //r.GET("/demo02",myfunc.Hello ) //定义路径下页面,通配,页面需要加
    15. //r.GET("/demo02",myfunc.Hello2 )
    16. r.GET("/demo02",myfunc.Hello07)
    17. // define end标签
    18. r.Run()
    19. }

    函数单独写一个页面

    1. package myfunc
    2. import "github.com/gin-gonic/gin"
    3. func Hello(context *gin.Context) {
    4. //context.String(300,"这是我的第一个gin")
    5. name := "你好我是golang" //将要渲染的字符串传入,在页面上使用上下文获取{{.}}
    6. context.HTML(200,"demo02/hello02.html",name)
    7. //加载的html路径
    8. }
    9. type Student struct {
    10. Name string
    11. Age int64
    12. }
    13. func Hello2(context *gin.Context) {
    14. s := Student{
    15. Name: "goland" ,
    16. Age: 2018,
    17. }
    18. context.HTML(300,"demo02/hello02.html",s)
    19. }
    20. func Hello3(context *gin.Context) {
    21. var arr [3]int
    22. arr[0] = 10
    23. arr[1] = 20
    24. arr[2] = 30
    25. context.HTML(400,"demo02/hello02.html",arr)
    26. }
    27. func Hello5(context *gin.Context){
    28. var a map[string]int = make(map[string]int,3)
    29. a ["lili"] = 18
    30. a["菲菲"] =12
    31. a["毛毛"] =3
    32. context.HTML(200,"demo02/hello02.html",a)
    33. }
    34. func Hello6(context *gin.Context) {
    35. var a map[string]Student = make(map[string]Student,3)
    36. a["No1"]= Student{
    37. Name:"毛毛",
    38. Age: 2,
    39. }
    40. a["No2"]= Student{
    41. Name:"菲菲",
    42. Age:21,
    43. }
    44. context.HTML(200,"demo02/hello02.html",a)
    45. }
    46. func Hello07(context *gin.Context) {
    47. slice := []int{1,2,3,4,5}
    48. context.HTML(200,"demo02/hello02.html",slice)
    49. }

    网页样式写一个页面

    1. {{ define "demo02/hello02.html"}}
    2. "en">
    3. "UTF-8">
    4. Title
    5. "stylesheet" href="/s/css/mycss.css">
    6. 这是hello02的页面
    7. 这部分我想要红色
    8. {{/* 这部分我想要红色 */}}
    9. {{/*{{.Name}}
      */
      }}
    10. {{/*{{.Age}}*/}}
    11. {{/*{{range .}}*/}}
    12. {{/*第一个点代表的是传入的数组的上下文,第二个点代表的变量的数组的上下文*/}}
    13. {{/*{{.}}*/}}
    14. {{/*{{end}}*/}}
    15. {{/*第二种方式,带下标*/}}
    16. {{/*{{range $i,$v := .}}*/}}
    17. {{/*{{$i}}*/}}
    18. {{/*{{$v}}*/}}
    19. {{/*{{end}}*/}}
    20. {{/*获取map中的内容,通过key获取value*/}}
    21. {{/*{{.lili}}*/}}
    22. {{/*{{.毛毛}}*/}}
    23. {{/*.是上下文,key得到value*/}}
    24. {{/*{{.No1.Name}}*/}}
    25. {{/*{{.No2.Age}}*/}}
    26. {{range .}}
    27. {{.}}
    28. {{end}}
    29. {{range $i ,$v := .}}
    30. {{$i}}
    31. {{$v}}
    32. {{end}}
    33. {{end}}
    这是hello02的页面 这部分我想要红色 1 2 3 4 5 0 1 1 2 2 3 3 4 4 5

    运行结果,数组和切片可以通过{{rang ,}} {{.}}{{end}} 来实现遍历

    其他如 map,struct可以使用键值对

  • 相关阅读:
    数仓学习笔记(2)——业务数据采集
    大数据知识点之什么是大数据
    深入理解Python虚拟机:super超级魔法的背后原理
    MySQL--函数
    [C++] 动态内存-malloc/free和new/delete
    Linux系统编程——总结初识Linux(常用命令、特点、常见操作系统)
    STM8应用笔记5.8位定时器应用之一
    机器学习-10-基于paddle实现神经网络
    社区系统项目复盘-2
    新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
  • 原文地址:https://blog.csdn.net/ens160/article/details/134407514