pixel
pixel

Reputation: 26483

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?

Upvotes: 8

Views: 9725

Answers (1)

Thundercat
Thundercat

Reputation: 121019

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

Run it on the playground.

Upvotes: 13

Related Questions