“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

preface

The basic page is complete. I tried to optimize the page using Flex layout in the previous article, and now WAIT for the complete data before refining it. Complete data bound to a database to save, if enough time to get a background behind, now if you need to do an interface to the database data displayed through web framework, I choose is Gin framework, because it is the foundation, the initial function of the content is not much, is simple can be up and running, and the performance is higher, I also wrote several articles about Golang before, and made a simple audio and video synthesis interface. If you are interested, you can go and have a look.

The installation of Golang

Golang is easy to install, similar to Nodejs. You can download the installation package from the official website and install it directly

Official website Address:

I installed 1.17.1 Domestic Image accelerated configurationgo versionView version:Of course, as with Nodejs, you need to change third-party dependencies:

go env -w GOPROXY=https://goproxy.cn,direct
Copy the code

Need to open the Go Module:

set GO111MODULE=on
Copy the code

Enabling the Go Module enables the official package management tool. If you do not initially enable the go Module, all packages will be in the default Golang installation directory, which does not solve the problem of environment differences. Turn it on and Go will ignore the GOPATH and Vendor folders and only download dependencies according to go.mod

Gin to start

Gin is very simple and can be started with a single file. Let’s first try creating a new main.go file and enter the following contents:

package main

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

func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	r.Run()
}

Copy the code

Initialize the project dependencies with Go mod init main, and you can see that the go.mod file is generated to record the project dependencies. Then download the Gin framework dependency file:

go get -u github.com/gin-gonic/gin
Copy the code

After this command is executed, it is possible to create a new version of go.sum, and go.mod adds a lot of third party dependencies. The go.sum record contains the expected encrypted hash of the content of a particular module version, ensuring that future downloads of these modules retrieve the same bits as the first download without us manually maintaining it; Go.mod automatically records some dependent versions. Now we start it in the project root directory:

go run main.go
Copy the code

The go run command automatically compiles the mian. Go file into a binary file and executes it. The binary file is not generated in the run directory, but is executed in a temporary file.

Now let’s visit:http://localhost:8080/ping

The access was successful and received a processing response from the Gin framework ~

conclusion

Having simply run Gin from scratch, let’s simply create a new route using GORM to connect to the database. Design the database.