First, in URLs the &
character has special meaning: it spearates query parameters. So if the data you encode in the URL contains &
characters, it has to be escaped. url.QueryEscape()
can show you how the escaped form looks like:
fmt.Println(url.QueryEscape("Shop & stop "))
It outputs:
Shop+%26+stop+
Note that &
is escaped like %26
, and also spaces are substituted with a +
sign.
So your valid input URL should look like this:
rawURL := "http://localhost:8080/myAPI?name=Shop+%26+stop+#23452"
Use the net/url
package to parse valid URLs, it also supports getting the parameter values and the fragment you're looking for (the fragment is the remaining part after the #
character):
u, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
fmt.Println(u.Query().Get("name"))
fmt.Println(u.Fragment)
This outputs:
Shop & stop
23452
Try the examples on the Go Playground.