• gin读取静态文件内容


    测试准备两个txt文件,内容随意,在文件static/json文件夹下, homeTab.txt,searchKey.txt

    启动入口
    main.go

    package main
    
    import (
    	"fmt"
    	"gin-test/router"
    )
    
    func main() {
    	// 初始化路由并启动
    	router.InitRouter()
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    路由配置
    router/router.go

    package router
    
    import (
    	"gin-test/controller"
    
    	"github.com/gin-gonic/gin"
    )
    
    func InitRouter() {
    	// 创建路由
    	r := gin.Default()
    
    	// 读取静态资源
    	r.Static("/static", "./static")
    
    	// 创建一个emp路由组
    	empRouter := r.Group("/emp")
    
    	// 测试路由组
    	empRouter.GET("/test", controller.EmpController{}.Index)
    
    	// 获取搜索关键词数据
    	empRouter.GET("/searchKeyWord", controller.EmpController{}.SearchKeyContent)
    
    	// 获取首页tab信息
    	empRouter.GET("/getTabInfo", controller.EmpController{}.HomeTabInfos)
    
    	// 启动服务
    	r.Run(":9999")
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    控制器
    controller/empController.go

    package controller
    
    import (
    	"io/ioutil"
    	"net/http"
    
    	"github.com/gin-gonic/gin"
    )
    
    type EmpController struct{}
    
    // 测试内容
    func (EmpController) Index(c *gin.Context) {
    	c.JSON(http.StatusOK, gin.H{
    		"message": "hello world",
    	})
    }
    
    // 获取搜索内容数据
    func (EmpController) SearchKeyContent(c *gin.Context) {
    	// 读取文件searchKey.txt内容
    	searchKeyContent, err := ioutil.ReadFile("static/json/searchKey.txt")
    
    	if err != nil {
    		c.JSON(http.StatusOK, gin.H{
    			"code":     1,
    			"msg":      "error",
    			"errorMsg": err.Error(),
    		})
    		return
    	}
    
    	c.JSON(http.StatusOK, gin.H{
    		"code": 0,
    		"data": string(searchKeyContent),
    		"msg":  "success",
    	})
    }
    
    // 获取首页tan名称以及跳转信息
    func (EmpController) HomeTabInfos(c *gin.Context) {
    	// 读取文件searchKey.txt内容
    	searchKeyContent, err := ioutil.ReadFile("static/json/homeTab.txt")
    
    	if err != nil {
    		c.JSON(http.StatusOK, gin.H{
    			"code":     1,
    			"msg":      "error",
    			"errorMsg": err.Error(),
    		})
    		return
    	}
    
    	c.JSON(http.StatusOK, gin.H{
    		"code": 0,
    		"data": string(searchKeyContent),
    		"msg":  "success",
    	})
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
  • 相关阅读:
    Unity三维数学总结
    【JavaScript】Math对象知识全解
    Nginx 监控模块
    SSM花艺商城系统毕业设计-附源码171536
    【Python】Pandas数据处理教程
    Canal使用和安装总结
    Linux常用操作集合(三)
    【MATLAB第71期】基于MATLAB的Abcboost自适应决策树多输入单输出回归预测及多分类预测模型(更新中)
    Python3用OpenCV4连接图像
    数据库 1.关系
  • 原文地址:https://blog.csdn.net/qq_44472790/article/details/134271045