Gin Gonic - colon within URL
Gin Gonic is a popular Go language web framework that is widely used to build high-performance web applications. When using Gin Gonic, sometimes we need to use colons in the URL path to define parameters, but by default Gin Gonic does not support the use of colons in the URL. So, how do you use colons in URLs? In this article, PHP editor Apple will introduce a simple and effective method to solve this problem so that our Gin Gonic application can support colons in URLs.
Question content
I am creating some REST API using Gin Gonic in Go.
I have to expose this endpoint using the following URL:
/api/v1/action::request::export
I'm using gin gonic to create a route but I'm getting this error "Only one wildcard is allowed per path segment" because the colon ":" is used to map parameters in the URL.
Is there a way to escape the ":" character and use it in URLs?
Thank you
Solution
Like f.s mentioned in the comments, you can just use one pathparam action
Then parse it as needed in your code.
Here's an example for you:
package main import ( "strings" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.GET("/api/v1/:action", func(c *gin.Context) { params, ok := c.Params.Get("action") if !ok { // handle error } eachParam := strings.SplitN(params, ":", 3) request, export := eachParam[1], eachParam[2] // your actual params divided by ":" c.JSON(200, gin.H{ "message": "good", }) }) r.Run() }
But of course, this approach has its own caveats, and you'll have to handle exceptions and edge cases yourself.
The above is the detailed content of Gin Gonic - colon within URL. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...

Why does map iteration in Go cause all values to become the last element? In Go language, when faced with some interview questions, you often encounter maps...

How to achieve the 45-degree curve effect of segmenter? In the process of implementing the segmenter, how to make the right border turn into a 45-degree curve when clicking the left button, and the point...
