Go和Beego都支持像id=这样的动态URL路由。

huangapple go评论94阅读模式
英文:

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

huangapple
  • 本文由 发表于 2017年2月16日 20:50:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/42274490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定