1.Route:
func main() { r := () ("/ping/:name", func(c *) { (200, { "message": ("name"), }) (("name")) }) () // listen and serve on 0.0.0.0:8080 }
If the url parameter is set in the form of a colon, it can be obtained by passing the corresponding parameters. But there is a problem. Can't ping itself
func main() { r := () ("/ping/*name", func(c *) { (200, { "message": ("name"), }) (("name")) }) () // listen and serve on 0.0.0.0:8080 }
There is another parameter form here, that is, *. Each parameter obtained will contain /, which can also be used on /ping. If the processing form of /ping does not exist in the current route, it will automatically jump to /ping/ and hand it over to /ping/*name for processing.
Moreover, /ping/*name and /ping/:name conflict, and routes cannot be written to at the same time.
2. Transfer the parameters
func main() { r := () ("/ping", func(c *) { (200, { ":message": ("name"), }) }) () // listen and serve on 0.0.0.0:8080 }
When get parameter transmission (parameters displayed on url)
You can get the corresponding value by doing it. If you need to determine whether it is empty, you can use it more elegantly, or you can use getOrDefault similar to map in java
When POST pass parameter (request to write body)
You can get the corresponding value by doing so. If you need to determine whether it is empty, you can use it more elegantly, or you can use getOrDefault similar to map in java.
However, during the test, I found that this method does not seem to be able to obtain the corresponding JSON like the RequestBody in SpringMVC. The operation here will be a bit complicated, and it requires reading the Body through the stream, converting it to JSON, and reading the corresponding parameters through the map
var user map[string]interface{} body, _ := () (body, &user) ("Get username in json:", user["username"]) ("Get password in json:", user["password"].(string)) //Turn the string to determine the length by len(password)!=0!
Here is a question about the above method, let’s first ask a question to see if it can be solved in the subsequent learning. It cannot directly implement a read conversion through middleware. From SpringMVC, this is not a very difficult wheel.
This is the article about GIN routing and transmission of parameters. For more related GIN routing and transmission of parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!