How to obtain the last path segment of a URL
Asked Answered
P

1

7

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?

Puree answered 21/12, 2019 at 17:56 Comment(0)
T
12

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

Run it on the playground.

Transport answered 21/12, 2019 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.