Google App Engine与Golang:如何将URL路径段解析为变量?

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

Google App Engine with Golang: How do you parse URL path segments as variables?

问题

在Google App Engine中使用Go语言,我想将以下URL:

http://www.example.com/api/account/123456/product/573832

处理成这样:

http://www.example.com/api/account/{acctId}/product/{prodId}

然后在我的处理函数中访问acctIdprodId

我该如何实现这个功能?

英文:

In Google App Engine with Go, I would like to take a URL like this:

http://www.example.com/api/account/123456/product/573832

and treat it like this:

http://www.example.com/api/account/{acctId}/product/{prodId}

Then access acctId and prodId in my handler function.

How do I do this?

答案1

得分: 8

这是你要翻译的内容:

func httpHandle(httpResponse http.ResponseWriter, httpRequest *http.Request) {
    urlPart := strings.Split(httpRequest.URL.Path, "/")
    // urlPart[3] 是 acctId,urlPart[5] 是 prodId
}

请注意,我只翻译了代码部分,其他内容都被忽略了。如果你还有其他问题,请告诉我。

英文:

There you are:

func httpHandle(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	urlPart := strings.Split(httpRequest.URL.Path, "/")
	// urlPart[3] is the acctId, urlPart[5] is the prodId
}

答案2

得分: 1

httprouter似乎快速且简单。如果你需要一个更高级、复杂的路由框架,那就去找别的吧。

go get github.com/julienschmidt/httprouter

然后:

package goseo

import (
    "fmt"
    "net/http"
    "github.com/julienschmidt/httprouter"
)

func init() {
    router := httprouter.New()
    router.GET("/", index)
    http.Handle("/", router)
}

func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Hello, world!")
}

示例匹配:

模式:/user/:user

  • /user/gordon 匹配
  • /user/you 匹配
  • /user/gordon/profile 不匹配
  • /user/ 不匹配

更多示例请参考:https://github.com/julienschmidt/httprouter

此外,我有一个app.yaml文件:

application: student-course-review
module: goseo
version: goseo1
runtime: go
api_version: go1

handlers:
- url: /static/(.+)
  static_files: static/
  upload: static/(.*)

- url: /.*
  script: _go_app
英文:

httprouter seems to be fast and simplistic. If you need a more advanced sophisticated routing framework then look elsewhere?

go get github.com/julienschmidt/httprouter

then:

package goseo

import (
    "fmt"
    "net/http"
    "github.com/julienschmidt/httprouter"
)

func init() {
    router := httprouter.New()
    router.GET("/", index)
    http.Handle("/", router)
}

func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Hello, world!")
}

example matches:

Pattern: /user/:user

 /user/gordon              match
 /user/you                 match
 /user/gordon/profile      no match
 /user/                    no match

further examples:

https://github.com/julienschmidt/httprouter

Further, I have an app.yaml:

application: student-course-review
module: goseo
version: goseo1
runtime: go
api_version: go1

handlers:
- url: /static/(.+)
  static_files: static/
  upload: static/(.*)

- url: /.*
  script: _go_app

答案3

得分: -1

你可能想考虑使用一个库,比如"httprouter"来实现这个功能。这个指南可能会帮助你选择一个合适的库。

英文:

You might want to consider using a library, like "httprouter" for this. This guide might help you choose one.

huangapple
  • 本文由 发表于 2014年10月10日 11:07:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/26291336.html
匿名

发表评论

匿名网友

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

确定