SoFunction
Updated on 2025-03-05

Detailed explanation of basic Gin routing usage of golang microservice framework

Overview

Routing is a custom URL address to execute specified functions. Good routing definition can have a good effect on SEO.

1. Basic routing

The gin framework encapsulates the http library and provides http request methods such as GET, POST, PUT, DELETE, PATCH, HEAD, and OPTIONS.

use()To bind the route

 func (group *RouterGroup) METHOD(relativePath string, handlers ...HandlerFunc) IRoutes
router := ()
("/get", func(c *) { (200, {"message": "get method"}) })
("/post", func(c *) { (200, {"message": "post method"}) })
("/put", func(c *) { (200, {"message": "put method"}) })
("/delete", func(c *) { (200, {"message": "delete"}) })
("/patch", func(c *) { (200, {"message": "patch"}) })
("/head", func(c *) { (200, {"message": "head"}) })
("/options", func(c *) { (200, {"message": "options"}) })
(":9999")//Specify port localhost:9999

2. Routing parameters

Get all parameters of the URL path

With / as the splitter, each parameter represents a dynamic variable with ":" as the parameter, and will be automatically bound to the corresponding parameters of the route.
Routing rules: [:] means that no matching is required

for example:

http://localhost:8080/user/Li Si/20/Beijing/Male will match "http://localhost:8080/user/:name/:age/:address/:sex"

In the above link, you can use the above

Use /user/:name/:age/:address/:sex to match Li Si, 20, Beijing, and men respectively

("key")
//http://localhost:8080/user/Li Si/20/Beijing/Male("/user/:name/:age/:address/:sex", func(c *) {
    //Print all parameters in the URL    //"[{name Li Si} {age 20} {address Beijing} {sex male}]\n"    (, ())
})

Note: But it won't match /user/ or /user

Visit: http://localhost:8080/user/Li Si/20/Beijing/Male

result:

"[{name Li Si} {age 20} {address Beijing} {sex male}]\n"

Get a single parameter of the URL path

Use the Param(key) method of the object to get the value of a certain key, and the method declaration is as follows:

//http://localhost:8080/login/15949629528/123456
("/login/:name/:password", func(c *) {
  (, {
    //{ name: "15949629528", password: "123456" }
    "name":     ("name"),
    "password": ("password"),
  })
})

Visit: http://localhost:8080/login/15949629528/123456

result:

{ name: "15949629528", password: "123456" }

Get the specified parameters in the URL

GET, POST request

Getting the path value in the URL is different from getting the parameters

for example:

http://localhost:8080/login?name=Zhang San&password=123456

You can use the following method to get the values ​​of the request parameters name and password.

//Return the value of the key in the URLfunc (c *Context) Query(key string) string
//GET request("/login", func(c *) {
  //{ name: "Zhang San", password: "123456" }  (, {
    "name":     ("name"),
    "password": ("password"),
  })
})

//POST request("/login", func(c *) {
	//{"name":"Zhang San","password":"123456"}	(, {
		"name":     ("name"),
		"password": ("password"),
	})
})

Visit: http://localhost:8080/login?name=Zhang San&password=123456

The output content is as follows:

{ name: "Zhang San", password: "123456" }

Get the parameter with the specified default value

Receive GET, POST requests with default values

Of course, the gin framework also thought of this point.()Method allows you to specify the received parameter name and the default value set when the parameter value is not received, and the declaration is as follows:

func (c *Context) DefaultQuery(key, defaultValue string) string

Only when the request does not carry the key, the default value will take effect at this time. In other cases, the default value does not take effect. Even if the value of the key in the URL is empty, the default value will not be enabled, and the obtained value is empty.

Note that this is to get the parameter value in the URL

//GET request("/user", func(c *) {
  //{ name: "Zhang San", password: "123456" }  (, {
    "name":     ("name", "Default Zhang San"),
    "password": ("password", "Default Password"),
  })
})
//POST request("/user", func(c *) {
//{"name":"Zhang San","password":"Default Password"}	(, {
	  "name":     ("name", "Default Zhang San"),
	  "password": ("password", "Default Password"),
	})
})

Visit: http://localhost:8080/user?password=

The output content is as follows:

{ name: "Default Zhang San", password: "Default Password" }

The above is the detailed explanation of the use of Gin basic routing in the golang microservice framework. For more information about Gin basic routing, please pay attention to my other related articles!