dro59505 2016-01-08 01:16
浏览 2907
已采纳

将URL路径与path.Join()合并

Is there a way in Go to combine URL paths similarly as we can do with filepaths using path.Join()?

For example see e.g. Combine absolute path and relative path to get a new absolute path.

When I use path.Join("http://foo", "bar"), I get http:/foo/bar.

See in Golang Playground.

  • 写回答

4条回答 默认 最新

  • drsl90685154 2016-01-08 01:33
    关注

    The function path.Join expects a path, not a URL. Parse the URL to get a path and join with that path:

    u, err := url.Parse("http://foo")
    u.Path = path.Join(u.Path, "bar.html")
    s := u.String() // prints http://foo/bar.html
    

    <kbd>playground example</kbd>

    If you are combining more than the path (scheme or host for example) or the string is more than the path (it includes a query string for example), then use ResolveReference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?