Search code examples
gopathurisegment

How to obtain the last path segment of a URL


I have following url:

targetUrl := "http://google.com/foo/bar?a=1&b=2"
myUrl, err := url.Parse(targetUrl)

I wonder how I can obtain last path segment (bar) from myUrl.Path?


Solution

  • Use path.Base to get the last element of a path: path.Base(myUrl.Path)

    Run it on the playground.