Golang's web framework Gin (1)---Gin installation and first experience

Introduction

1.1 Introduction

The most popular web framework in the Go world, with 32K+ stars on Github. Web framework developed based on httprouter. Chinese documentation is complete, easy-to-use lightweight framework.

  1. Gin is a golang micro-framework with elegant encapsulation, friendly API, clear source code comments, fast, flexible, fault-tolerant and convenient, etc.
  2. For golang, the dependency of the web framework is much smaller than that of Python, Java and the like. Its own net/http is simple enough and its performance is very good
  3. With the help of framework development, not only can save a lot of time brought by commonly used packages, but also help the team's coding style and form norms

Gin features:

  1. Fast speed: Routing based on radix tree takes up less memory. There is no reflection. Predictable API performance. Middleware support: incoming HTTP requests can be handled by middleware chains and final actions.
  2. Crash-free: Gin can catch a panic that occurs during an HTTP request and recover it. This way, your server will always be available.
  3. JSON Validation: Gin can parse and validate the requested JSON - for example, check for the presence of required values
  4. Route Grouping: Better organize your routes. Authorization required vs. not required, different API versions... Also, groups can be nested infinitely without penalizing low performance. error management
  5. Gin provides a convenient way to collect all errors that occur during HTTP requests. Eventually, middleware can write them to log files, databases and send them over the network.
  6. Built-in rendering: Gin provides an easy-to-use API for JSON, XML and HTML rendering
  7. Extensible: creating a new middleware is very simple, just look at the example code

1.2 Installation

To install the Gin package, you need to have Go installed and first set up a Go workspace.
1. First you need to install Go (requires version 1.10+), then you can use the following Go command to install Gin.

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

If the download fails, proxy settings may be required

  • under windows
# 设置goproxy.io代理
go env -w GOPROXY="https://goproxy.io"
# 设置GO111MOUDLE
go env -w GO111MODULE="on"
  • under mac/linux
# 设置goproxy.io代理
export GOPROXY=https://goproxy.io
# 设置GO111MOUDLE
export GO111MODULE=on 

2. Check go.mod (equivalent to maven's pom.xml)
to see if the dependencies are downloaded successfully
insert image description here
3. Write a simple example and verify whether the gin framework is successfully built

package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
)
func main() {
    
    
	//创建一个服务
	ginServer := gin.Default()

	//访问地址,处理我们的请求 Request Response
	//gin.Context,封装了request和response
	ginServer.GET("/hello", func(context *gin.Context) {
    
    
		context.JSON(200, gin.H{
    
    
			"msg": "hello world",
		})
	})
	//服务器端口
	//Run("里面不指定端口号默认为8080") 
	ginServer.Run(":8081")

Test in browser:
insert image description here
ok

Guess you like

Origin blog.csdn.net/m0_49683806/article/details/128995590