At Go, the package pathis your friend.
You can get a directory or folder from a path using path.Dir(), for example
p := "/xyz/index.php"
dir := path.Dir(p)
fmt.Println("dir:", dir) // Output: "/xyz"
If you find a link with a root path (starts with a slash), you can use it as is.
If it is relative, you can join it using the dirabove using path.Join(). Join()also "clear" the URL:
p2 := path.Join(dir, "index.php")
fmt.Println("p2:", p2)
p3 := path.Join(dir, "./index.php")
fmt.Println("p3:", p3)
p4 := path.Join(dir, "../index.php")
fmt.Println("p4:", p4)
Conclusion:
p2: /xyz/index.php
p3: /xyz/index.php
p4: /index.php
"", path.Join(), path.Clean(), , . :
- .
. ( )... path name ( ) .., ..., , "/.." "/" .
"" URL ( , ..), url.Parse() url.URL raw url, URL- , :
uraw := "http://example.com/xyz/index.php"
u, err := url.Parse(uraw)
if err != nil {
fmt.Println("Invalid url:", err)
}
fmt.Println("Path:", u.Path)
:
Path: /xyz/index.php
Go Playground.