Home  >  Article  >  Backend Development  >  Go gin: How to read multiple route segments?

Go gin: How to read multiple route segments?

WBOY
WBOYforward
2024-02-09 15:39:09793browse

Go gin:如何读取多个路线段?

php editor Baicao brings you a practical tutorial on the Go gin framework - "Go gin: How to read multiple route segments?". When using Go gin for web development, we often need to read the parameters of multiple route segments. This article will introduce in detail how to implement this function through the Go gin framework. Whether you are a beginner or an experienced developer, this tutorial can help you better understand and apply the Go gin framework and improve your development efficiency. Let’s explore together!

Question content

I added the PUT /:folder route to create the folder

I need a PUT /:folder/:path/to/final/file to be used when the user posts a new file. So I have the root in the first parameter, but I don't know how to create a route that handles 'n' route segments and read it into a single string

For example call

PUT /cats

A folder named cats will be created. This already works

I need to

PUT /cats/milady/first-year/32312.jpg

Recognize "cats" as the first parameter, which is my user-level folder Then check mylady/firstyear as a nested subfolder and create

if necessary

and 32312.jpg file name

How to use gin to set the route? Place

Solution

I found the answer.

I can create groups so under the group I can use an asterisk for "undefined path level"

superGroup := router.Group("/:folder")
{
        
    // Create a folder
    superGroup.PUT("", createFolder)

    // Save file into folder
    superGroup.PUT("/*full-path", uploadFile)
}

Then I can read full-path and folder

func uploadFile(c *gin.Context) {

    folder:= c.Param("folder")
    fullPath := c.Param("full-path")

    ....

    c.Status(http.StatusOK)
}

The above is the detailed content of Go gin: How to read multiple route segments?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Buffer channel size limitNext article:Buffer channel size limit