英文:
How to avoid end of URL slash being removed when ResolveReference in Go
问题
在下面的示例中,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
英文:
In the following example, end of URL / is removed, is there a way to keep the /?
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))
}
Output:
http://localhost:5100/hello
答案1
得分: 1
我找到了答案,就是不使用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/
英文:
I figured out the answer, which is not to use 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))
}
Output:
http://localhost:5100/hello/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论