Golang uses the GIN framework to process requests
Use the generic Handle method
r := gin.Default()
r.Handle("GET", "/hello", func(context *gin.Context) {
name := context.DefaultQuery("name", "默认值")
fmt.Println(name)
context.Writer.Write([]byte("hello," + name))
})
r.Run()
The first parameter is the request method, such as GET or POST, the second parameter is the address of the request, and the third parameter is the method to process the request.
Use http://localhost:8080/hello
Output hello, the default value. Use context.defaultQuery (“name”, “default “) to get the name argument. If no name is passed, the second argument is used as the default, and if it is passed, the passed value is used
Use http://localhost:8080/hello? Name =fq, output hello,fq.
Take another example of a POST request
r := gin.Default()
r.Handle("POST", "/hello", func(context *gin.Context) {
name := context.PostForm("name")
fmt.Println(name)
context.Writer.Write([]byte("hello," + name))
})
r.Run()
We use context.postform (“name”) to get the parameters.
Using http://localhost:8080/hello, in the post the name value of fq
Hello, output fq.
- Use classification methods
r.Handle(“GET”, “/hello”, func(context *gin.Context) {})
This corresponds to the first argument as the method name, and only the second and third arguments.
r.GET(“/hello”, func(context *gin.Context) {})