给定两个绝对URI,找到它们之间的相对路径

给定两个绝对URI,找到它们之间的相对路径

问题描述:

Is there a function in go standard library that lets me do this

a = 'www.my.com/your/stuff'
b = 'www.my.com/your/stuff/123/4'

function(b,a) // /123/4 

or

function(URL(b),URL(a)) // /123/4

The following is probably defined in this case

function(a,b) // error ? or ../../

I'm aware that I can use path package for this. But it cannot work in many cases where there is query param, file extension etc.

Basically I'm looking for a path.resolve counterpart for URL

go标准库中是否有一个函数可以让我做到这一点 p>

   a ='www.my.com/your/stuff'
b ='www.my.com/your/stuff/123/4'

function(b,a)// / 123/4 \  n  code>  pre> 
 
 

或 p>

 功能(URL(b),URL(a))// / 123/4 \  n  code>  pre> 
 
 

在这种情况下可能定义了以下内容 p>

  function(a,b)//错误? 或../../ 
  code>  pre> 
 
 

我知道我可以为此使用 path code>包。 但这在存在查询参数,文件扩展名等的许多情况下不起作用。 p>

基本上,我正在寻找 path.resolve code>对应的URL p> div>

It turns out that the path/filepath package can do this for you. If you ignore the fact that these are URLs and instead treat them like paths, you can use filepath.Rel():

package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    base := "www.my.com/your/stuff"
    target := "www.my.com/your/stuff/123/4"
    rel, _ := filepath.Rel(base, target)
    fmt.Println(rel) // prints "123/4"
}

Playground: https://play.golang.org/p/nnF9zfFAFfc

If you want to treat these paths as actual URLs, you should probably use the net/url package to first parse the path as a URL, then extract the path and use filepath.Rel() on that. This allows you to properly deal with things like queries in the URL string, which would trip up filepath, like so:

package main

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

func main() {
    url1, _ := url.Parse("http://www.my.com/your/stuff")
    url2, _ := url.Parse("http://www.my.com/your/stuff/123/4?query=test")

    base := url1.Path
    target := url2.Path

    rel, _ := filepath.Rel(base, target)
    fmt.Println(base)   // "/your/stuff"
    fmt.Println(target) // "/your/stuff/123/4"
    fmt.Println(rel)    // "123/4"
}

Playground: https://play.golang.org/p/gnZfk0t8GOZ

As a bonus, filepath.Rel() is smart enough to handle relative paths in the other direction, too:

rel, _ = filepath.Rel(target, base) // rel is now "../.."