英文:
Does Go or Beego supports dynamic url routing like id=?
问题
func main() {
beego.Router("/", &MainController{})
beego.Router("/userid/", &SqlController{})
beego.Run()
}
这段代码可以处理"http://localhost:8080/userid"这样的URL,但是如果我想要用户ID的值是动态的,比如"http://localhost:8080?userid=1",我不知道如何在Go语言中使用路由来实现这个。
英文:
func main() {
beego.Router("/", &MainController{})
beego.Router("/userid/", &SqlController{})
beego.Run()
}
this works fine for url "http://localhost:8080/userid"
but if i want user id value to be dynamic for ex "http://localhost:8080?userid=1"
i could not how o achieve this using router in go.
答案1
得分: 1
从?id=xxx
中提取xxx
是关于请求参数解析的,你可以在beego的文档中找到示例。
路由(在你的上下文中)是将匹配某种模式的请求映射到相应操作的过程。当id不同时,我猜你想要的不是将它们映射到不同的操作。所以它不应该被称为路由。它只是参数解析。
英文:
Extracting xxx
from ?id=xxx
is about request parameter parsing, you can get examples in beego's document.
Routing (in your context) is about mapping requests matching a certain pattern to corresponding actions. When id is different, I suppose what you want is not getting them mapped to different actions. So it shouldn't be called routing. It's just parameter parsing.
答案2
得分: 0
不确定beego,但是使用go http请求,你可以像这样访问查询参数:request.URL.Query()
,其中request
的类型是*http.Request
。你想要的基本上是从URL获取查询参数?所以你从请求中获取URL对象,然后访问查询参数。Query()
方法返回一个map[string][]string
。
英文:
Not sure about beego, but using go http request you can access the query parameters like this
request.URL.Query()
, where request
is of type *http.Request
. What you want is basically query parameters from the URL? So you get the URL object from the the request and then access the query parameters. The Query()
method returns a map[string][]string
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论