What does gin mean?

藏色散人
Release: 2019-05-29 10:29:49
Original
31513 people have browsed it

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"
Copy after login

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")
}
Copy after login

Browser output:

5 xiaoming
Copy after login

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")
}
Copy after login

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
Copy after login

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

Related labels:
gin
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template