http - Go 中 ResolveReference 时如何避免 URL 结尾斜杠被删除

标签 http url path go

如下example ,URL 末尾 / 被删除,有办法保留 / 吗?

package main

import (
    "fmt"
    "net/url"
    "path"
)

func main() {
    u, _ := url.Parse("http://localhost:5100")
    relative, _ := url.Parse(path.Join("hello/"))
    fmt.Println(u.ResolveReference(relative))
}

输出:

http://localhost:5100/hello

最佳答案

我找到了答案,那就是不使用path.Join:

package main

import (
    "fmt"
    "net/url"
)

func main() {
    u, _ := url.Parse("http://localhost:5100")
    relative, _ := url.Parse("hello/")
    fmt.Println(u.ResolveReference(relative))
}

输出:

http://localhost:5100/hello/

关于http - Go 中 ResolveReference 时如何避免 URL 结尾斜杠被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23051339/

相关文章:

http - golang程序判断用户是否使用代理

php - 根据 url 路径重定向到不同的服务器

javascript - 如何在 AngularJS 中通过 http 调用实现多个 ng Controller ?

c - 提取coap查询

php - Yii2 yii\helpers\Url helpers 到另一个站点的 Url

python - 如何在 Django 中保护 URL

java - HttpURLConnection RequestPropertys 为空

java - exec-maven-plugin 说不能运行指定的程序,即使它在 PATH 上

java - 从左边第二个斜线后剪切字符串

http - Varnish Via 和 X-Varnish header : are they needed in production?