Home  >  Article  >  Backend Development  >  What does gin mean?

What does gin mean?

藏色散人
藏色散人Original
2019-05-18 14:29:4231514browse

What does gin mean?

Gin is a web framework written in Go and has the advantage of high performance.

1. Installation

Use go to download the gin library, command line input: go get github.com/gin-gonic/gin, generally use the required dependencies:

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

Two: Basic application

1.Query method in gin.Context: get URL parameter

package main
 
import (
    "github.com/gin-gonic/gin"
    "net/http"
)
 
func getQuery(context *gin.Context){
 
    userid := context.Query("userid")
    username := context.Query("username")
 
    context.String(http.StatusOK,userid+" "+username)
}
func main(){
    // 注册一个默认路由器
    router := gin.Default()
 
    //注册GET处理
    router.GET("/user", getQuery)
 
    //默认8080端口
    router.Run(":8088")
}

Browser output:

5 xiaoming

2.gin. Param method in Context: RESRful style URL parameter passing

package main
 
import (
    "github.com/gin-gonic/gin"
    "net/http"
)
 
func getParam(context *gin.Context){
 
    userid := context.Param("userid")
    username := context.Param("username")
 
    context.String(http.StatusOK,userid+" "+username)
}
func main(){
    // 注册一个默认路由器
    router := gin.Default()
 
    //注册GET处理
    //router.GET("/user", getQuery)
    router.GET("/user/:userid/:username",getParam)
    //默认8080端口
    router.Run(":8088")
}

Supplement: /:varname must match the corresponding one, /*varname must match all the following ones, and you cannot use more than one at the same time, otherwise a compilation error will be reported

Page output:

5 xiaoming

The above is the detailed content of What does gin mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn