Golang rapid development framework — adding gin framework (IV)


Golang rapid development framework — adding gin framework (IV)


The golang chapter of knowledge sharing is a record of all kinds of knowledge I learned when I use golang everyday. I will sort it out and share it with you in the form of articles for common learning. We welcome your continued attention.

The knowledge sharing series currently includes Java, golang, Linux, docker and so on.

development environment

  • System: windows10
  • Language: golang
  • Golang version: 1.17
  • Code warehouse:FastDevelopGo


We often need to use some basic components when we use golang to develop projects. Each new project is cumbersome, and the existing market feeling is not suitable for us. Therefore, we decided to build a set ourselves and open source it for everyone to use. We welcome you to put forward various needs.
Now let’s continue to improve the framework. The requirements to be completed in this section are:

  • increaseginFramework, which is convenient for our subsequent web development.

1. Install gin

go get -u github.com/gin-gonic/gin

2. Write the following code for initial use of the gin framework

package web

import (

type WebConfig struct {
    Host string `json:"host,omitempty"`
    Port string `json:"port,omitempty"`

//The web configuration file object is used for the initial use of global configuration
var webConfig WebConfig

func InitGin() {

//Initial web related configuration files and objects
func initGinConfig() {
    //Initial read configuration file
    if err := viper.UnmarshalKey("web", &webConfig); err != nil {
        log. Logger. Error ("fatal error reading configuration file:", zap. Error (ERR))

//Core startup gin framework function, main function
func startGin() {
    //Initialize basic configuration
    r := gin.Default()
    //Initialize gateway
    r.Run(webConfig.Host + ":" + webConfig.Port)
package router

func InitRouter() {
    //Todo here we initialize various gateway configurations
    //Todo initializes default static resources
    //Todo initializes the default exception handling gateway
    //Todo initializes the default template directory

3. Make supplementary web related configuration on the basis of the original configuration file

#This is the default configuration file

outputPaths=["stdout", "./tmp/logs"]
foo = "bar"

host = ""
port = "8080"

4. Start and test, observe the output of workbench log, and it has been used normally.

[GIN-debug] Listening and serving HTTP on

Next, I’m going to add related initial exception handling and static resource loading. You are welcome to continue to pay attention to the development of the framework.

In this framework, I will add visualization page, code quick generation module, project framework quick generation module, etc. for my initial ideas. If you have other requirements and ideas, you are welcome to leave a message in the comment area or directly put forward valuable issues in the code warehouse

Welcome to start actively. Your attention is my biggest motivation.

This document declares that:

Golang rapid development framework -- adding gin framework (IV)


Knowledge sharing license agreement
This work is byCn HuashaouseCreative Commons Attribution – non commercial use 4.0 international license agreementLicense.