Go gin: How to read multiple route segments?

WBOY
Release: 2024-02-09 15:39:09
forward
774 people have browsed it

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

A folder named cats will be created. This already works

I need to

PUT /cats/milady/first-year/32312.jpg
Copy after login

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

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

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!

source:stackoverflow.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!