如何避免在Go中的ResolveReference时删除URL斜杠的结尾

时间:2014-04-14 03:08:17

标签: http url path go

在以下example中,网址/的末尾已删除,是否有办法保留/

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

1 个答案:

答案 0 :(得分: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/