Here is a simple solution that works for URLs with the same structure as yours (you can improve to fit those with other structures)
package main import ( "fmt" "net/url" ) var path = "http://localhost:8080/id/123" func getFirstParam(path string) (ps string) { // ignore first '/' and when it hits the second '/' // get whatever is after it as a parameter for i := 1; i < len(path); i++ { if path[i] == '/' { ps = path[i+1:] } } return } func main() { u, _ := url.Parse(path) fmt.Println(u.Path) // -> "/id/123" fmt.Println(getFirstParam(u.Path)) // -> "123" }
Or, as @gollipher suggested, use the path package
import "path" func main() { u, _ := url.Parse(path) ps := path.Base(u.Path) }
With this method, it is faster than a regular expression if you know the structure of the URL that you receive.
source share