Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse URLs in the format of /id/123 not ?foo=bar

Tags:

go

I'm trying to parse an URL like:

http://example.com/id/123

I've read through the net/url docs but it seems like it only parses strings like

http://example.com/blah?id=123

How can I parse the ID so I end up with the value of the id in the first example?

This is not one of my own routes but a http string returned from an openid request.

like image 945
Datsik Avatar asked Aug 16 '15 05:08

Datsik


People also ask

How do you parse a link?

Method 1: In this method, we will use createElement() method to create a HTML element, anchor tag and then use it for parsing the given URL. Method 2: In this method we will use URL() to create a new URL object and then use it for parsing the provided URL.

What is parsing a URL?

URL parsing is a function of traffic management and load-balancing products that scan URLs to determine how to forward traffic across different links or into different servers. A URL includes a protocol identifier (http, for Web traffic) and a resource name, such as www.microsoft.com.

What is the rust URL?

rust-url is an implementation of the URL Standard for the Rust programming language.


1 Answers

You can try using regular expression as follow:

import "regexp"

re, _ := regexp.Compile("/id/(.*)")
values := re.FindStringSubmatch(path)
if len(values) > 0 {
    fmt.Println("ID : ", values[1])
}
like image 191
Prashant Thakkar Avatar answered Oct 12 '22 12:10

Prashant Thakkar