Introduction to Gin framework

1 Introduction

GinIt is an golangexcellent micro-framework, with more elegant and friendly encapsulation , clear source code annotations, fast, flexible, fault- tolerant APIand convenient features , etc .; It is simple enough, and its performance is also very good; with the help of framework development, it can not only save a lot of time brought by commonly used packaging, but also help the team's coding style and form norms.

golangwebPythonJavanet/http

2. Install the Gin framework

Prerequisite : Before installing Ginthe software package , you need to install Goand set up Gothe workspace , and you need to use Go 1.13and above versions.

2.1 View Go version

go version

insert image description here

2.2 Create a folder

insert image description here

2.3 Using vscodeOpen Folder

insert image description here

2.4 Use go modthe management project and generate go.modfiles

go mod init Gin

insert image description here

2.5 Download and install Gin

go get -u github.com/gin-goni

insert image description here
If the download fails, you can configure the proxy first.

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

After the download is successful, the following prompt will be displayed and a go.sumfile will be generated.
insert image description here

2.6 Newmain.go

package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)

func main() {
    
    
	// 1.创建路由
	r := gin.Default()
	// 2.绑定路由规则,执行的函数
	// gin.Context,封装了request和response
	r.GET("/", func(c *gin.Context) {
    
    
		c.String(http.StatusOK, "hello World!")
	})
	// 3.监听端口,默认在 0.0.0.0:8080 启动服务
	// Run("里面不指定端口号默认为8000")
	r.Run(":8000")
}

Run the project Open http://localhost:8000/
insert image description here
in the browser
insert image description here

Guess you like

Origin blog.csdn.net/weixin_51571728/article/details/127002215