-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurls.go
More file actions
37 lines (32 loc) · 881 Bytes
/
urls.go
File metadata and controls
37 lines (32 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package webtools
import "net/url"
// CreateURL creates a *url.URL from the given origin, path, and request
// parameters that has been properly encoded and formatted.
//
// resource url must be valid; an invalid url will panic.
func CreateURL(origin, path string, params map[string]string) *url.URL {
u, err := url.Parse(origin)
if err != nil {
// incoming resource URL should be known at compile time.
panic("web: cannot parse url " + origin)
}
// Set the URL path
u.Path = path
// set the query parameters
query := make(url.Values, len(params))
for k, v := range params {
query.Add(k, v)
}
u.RawQuery = query.Encode()
return u
}
// GetDomain extracts the domain name from a given url s.
//
// If s is not a valid url, the empty string is returned.
func GetDomain(s string) string {
u, uerr := url.Parse(s)
if uerr != nil {
return ""
}
return u.Host
}