gin 框架学习笔记(二) 返回一个静态网页

目录结构

1

apis

person.go

package api

import (  
    "net/http"

    "github.com/gin-gonic/gin"
)

// 返回HTML文件
func ShowHTML(c *gin.Context) {  
    c.HTML(http.StatusOK, "index.html", gin.H{
        "title": "this is home html",
    })
}

routers

routers.go

package routers

import (  
    . "github.com/study/gin_test/apis"

    "github.com/gin-gonic/gin"
)

func InitRouter() *gin.Engine {  
    routers := gin.Default()
    routers.LoadHTMLGlob("views/*")
    routers.GET("/", ShowHTML)
    return routers
}

main.go

package main

import (  
    routers "github.com/study/gin_test/routers"
)

func main() {  
    routers := routers.InitRouter()
    routers.Run(":6789")
}