核心知识点

  1. json配置文件解析成结构体
  2. 将路由对应的接口抽离到单独的文件中,main函数中直接注册路由即可

项目目录图

项目代码

  • app.json代码
{
"app_name": "cloudRestaurant",
"app_mode": "debug",
"app_host": "127.0.0.1",
"app_port": "8000"
}
  • Config.go代码,解析配置文件到结构体
package tool

import (
"bufio"
"encoding/json"
"fmt"
"os"
) type Config struct {
AppName string `json:"app_name"`
AppMode string `json:"app_mode"`
AppHost string `json:"app_host"`
AppPort string `json:"app_port"`
}
var _cfg Config func ParseConfig(path string) (*Config, error) {
// 解析配置文件
file, err := os.Open(path)
if err != nil {
panic(err.Error())
}
defer file.Close() reader := bufio.NewReader(file)
decoder := json.NewDecoder(reader)
err = decoder.Decode(&_cfg) // 注意:要将和json对应的结构体指针传进来,而不是结构体对象
if err != nil {
fmt.Println(err.Error())
return nil, err
} return &_cfg, nil }
  • HelloController.go文件,接口写在这里面
package controller

import (
"github.com/gin-gonic/gin"
"net/http"
) type HelloController struct {}
func (h *HelloController) Router(engine *gin.Engine) {
engine.GET("/hello", h.Hello)
engine.POST("/hello", h.HelloP)
}
func(h *HelloController) HelloP(ctx *gin.Context) {
ctx.Writer.Write([]byte("post hello"))
}
func(h *HelloController) Hello(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"msg": "OK",
})
}
  • main.go文件
package main

import (
"CloudRestaurant/controller"
"CloudRestaurant/tool"
"fmt"
"github.com/gin-gonic/gin"
) var (
config *tool.Config
)
func init() {
// 初始化配置文件
var err error
config, err = tool.ParseConfig("./config/app.json")
if err != nil {
fmt.Println(err.Error())
}
} func main() {
app := gin.Default() // 注册路由
registerRouter(app) app.Run(config.AppHost + ":" + config.AppPort)
} // 路由设置
func registerRouter(router *gin.Engine) {
new(controller.HelloController).Router(router)
}

gin框架中项目的初始化的更多相关文章

  1. gin框架路由拆分与注册

    gin框架路由拆分与注册 本文总结了我平时在项目中积累的关于gin框架路由拆分与注册的若干方法. gin框架路由拆分与注册 基本的路由注册 下面最基础的gin路由注册方式,适用于路由条目比较少的简单项 ...

  2. Go语言基础之20--web编程框架之Gin框架

    一.Gin框架介绍 1.1 简介 A. 基于httprouter开发的web框架. http://github.com/julienschmidt/httprouter B. 提供Martini风格的 ...

  3. Gin框架系列02:路由与参数

    回顾 上一节我们用Gin框架快速搭建了一个GET请求的接口,今天来学习路由和参数的获取. 请求动词 熟悉RESTful的同学应该知道,RESTful是网络应用程序的一种设计风格和开发方式,每一个URI ...

  4. 使用zap接收gin框架默认的日志并配置日志归档

    目录 使用zap接收gin框架默认的日志并配置日志归档 gin默认的中间件 基于zap的中间件 在gin项目中使用zap 使用zap接收gin框架默认的日志并配置日志归档 本文介绍了在基于gin框架开 ...

  5. Gin框架介绍与使用

    Gin // 初识 Gin框架 //下载(可能会下载不全.缺什么get什么即可) //go get -u -v github.com/gin-gonic/gin package main import ...

  6. golang(gin框架),基于RESTFUL的跨语言远程通信尝试

    golang(gin框架),基于RESTFUL的跨语言远程通信尝试 背景: 在今年的项目实训过程中,遇到了这样的问题: 企业老师讲课实用的技术栈是Java springboot. 实训实际给我们讲课以 ...

  7. 一步一步分析Gin框架路由源码及radix tree基数树

    Gin 简介 Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much ...

  8. 前端程序员学习 Golang gin 框架实战笔记之一开始玩 gin

    原文链接 我是一名五六年经验的前端程序员,现在准备学习一下 Golang 的后端框架 gin. 以下是我的学习实战经验,记录下来,供大家参考. https://github.com/gin-gonic ...

  9. 基于gin框架和jwt-go中间件实现小程序用户登陆和token验证

    本文核心内容是利用jwt-go中间件来开发golang webapi用户登陆模块的token下发和验证,小程序登陆功能只是一个切入点,这套逻辑同样适用于其他客户端的登陆处理. 小程序登陆逻辑 小程序的 ...

随机推荐

  1. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

  2. 【剑指Offer】反转链表 解题报告(Python)

    [剑指Offer]反转链表 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目描 ...

  3. 1007 - Mathematically Hard

    1007 - Mathematically Hard    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 6 ...

  4. lightoj 1102 - Problem Makes Problem

    1102 - Problem Makes Problem As I am fond of making easier problems, I discovered a problem. Actuall ...

  5. 比赛难度(HDU4546)

    比赛难度 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  6. Back to Underworld(lightoj 1009)

    1009 - Back to Underworld    PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 ...

  7. idea使用教程-模板的使用

    一.代码模板是什么 它的原理就是配置一些常用代码字母缩写,在输入简写时可以出现你预定义的固定模式的代码,使得开发效率大大提高,同时也可以增加个性化.最简单的例子就是在Java中输入sout会出现Sys ...

  8. Codeforces 931C:Laboratory Work(构造)

    C. Laboratory Work time limit per test : 1 second memory limit per test : 256 megabytes input : stan ...

  9. Linux Cgroups详解(一)

    [转载]http://blog.chinaunix.net/uid-23253303-id-3999432.html Cgroups是什么? Cgroups是control groups的缩写,是Li ...

  10. JS常见框架汇总

    基础框架 Vue.js 官网地址 : http://cn.vuejs.org/ 官方简介 : Vue.js 是一套用于构建用户界面的渐进式框架. 框架类型 : 前端项目级框架 适用平台 : 通用 仓库 ...