Install: Download and install

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

If the installation fails, open it directlygithub.com/gin-gonic/gin, download the zip package, and clickGOPATHPath created under the SRC directorygithub.com, unzip to the directory below;

Error message encountered:

Cannot find package “.” in:*******

Reinstall the missing dependencies once to fix the problem.

Go: Inconsistent vendoring in XXX error

Change GOPAHT to a non-Goroot path. GOROOT is the GO language installation path, GOPATH is the GO language compilation environment required path, so the two cannot be the same path

New project

Create a new folder app under SRC under the GOPATH directory and use the command

go mod init
Copy the code

Create main.go and write our first program

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

func main(a) {
	r := gin.Default()
	r.GET("/".func(c *gin.Context) {
		c.JSON(200, gin.H{
			"name": "Tell me Jayne to hide."."gend": "Male",
		})
	})
	r.Run(": 8080")}Copy the code

To run the program

go run main.go
Copy the code

Browser run:

Generate a JSON

func AsciiJSON(a) {
	r := gin.Default()
	r.GET("/".func(c *gin.Context) {
		c.JSON(200, gin.H{
			"name": "Tell me Jayne to hide."."gend": "Male",
		})
	})
	r.Run(": 8080")}Copy the code

call

package main
import (
	"app/function"
	"github.com/gin-gonic/gin"
)

func main(a) {
	// Use AsciiJSON to generate plain ASCII JSON with escaped non-ASCII characters.
	function.AsciiJSON()
}
Copy the code

Testing get requests

Bind form data requests to custom structures

// Bind form data requests to custom structures
type StructA struct {
	FieldA string `form:"field_a"`
}
type StructB struct {
	NestedStruct StructA
	FieldB       string `form:"field_b"`
}

/ / get request
func GetFormData(c *gin.Context) {
	var b StructB
	c.Bind(&b)
	c.JSON(200, gin.H{
		"a": b.NestedStruct,
		"b": b.FieldB,
	})
}
Copy the code

call

package main
import (
	"app/function"
	"github.com/gin-gonic/gin"
)

func main(a) {
	// Bind form data requests to custom structures
	r := gin.Default()
	// Test the GET request
	r.GET("/getFormData", function.GetFormData)
	r.Run()
}
Copy the code

Query Returned information

// Bind query string or publish data
type PersonInfo struct {
	Name    string `form:"name"`
	Address string `form:"address"`
	Memo    string `form:"memo"`
}

// Return information
func QueryInfo(c *gin.Context) {
	var person PersonInfo
	if c.ShouldBind(&person) == nil {
		log.Println(person.Name)
		log.Println(person.Address)
		log.Println(person.Memo)
	}
	c.String(200."Success")}Copy the code

call

package main
import (
	"app/function"
	"github.com/gin-gonic/gin"
)

func main(a) {
	r := gin.Default()
	// Query the returned information
	r.GET("/queryInfo", function.QueryInfo)
	r.Run()
}
Copy the code

Customize HTTP configuration

http.ListenAndServe(": 8080", router)
Copy the code

or

s := &http.Server{
	Addr:           ": 8080",
	Handler:        router,
	ReadTimeout:    10 * time.Second,
	WriteTimeout:   10 * time.Second,
	MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
Copy the code

Customize log files

router := gin.New()
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
	return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
			param.ClientIP,
			param.TimeStamp.Format(time.RFC1123),
			param.Method,
			param.Path,
			param.Request.Proto,
			param.StatusCode,
			param.Latency,
			param.Request.UserAgent(),
			param.ErrorMessage,
	)
}))
router.Use(gin.Recovery())
router.GET("/ping".func(c *gin.Context) {
	c.String(200."pong")
})
router.Run(8080 ":Copy the code

Reference documentation

1. If the GO language is inconsistent with air framework installation, the GO: Inconsistent Vendoring problem is encountered. 2. In 3. Warning: GOPATH set to GOROOT (/usr/local/go) has no effect 4. Gin official documentation 5