From aafc0b438156839e7c6f9486db4933e0270b5722 Mon Sep 17 00:00:00 2001 From: huzaifamk Date: Sun, 2 Jun 2024 18:20:49 +0500 Subject: [PATCH] Added functions for parsing url path and query params --- url_parse.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 url_parse.go diff --git a/url_parse.go b/url_parse.go new file mode 100644 index 0000000..7619eaf --- /dev/null +++ b/url_parse.go @@ -0,0 +1,23 @@ +package utilities + +import "strings" + +func ParseURLPath(url string) (path string, query string) { + qmIndex := strings.Index(url, "?") + if qmIndex == -1 { + return url, "" + } + return url[:qmIndex], url[qmIndex+1:] +} + +func ParseQueryParams(query string) map[string]string { + params := make(map[string]string) + pairs := strings.Split(query, "&") + for _, pair := range pairs { + kv := strings.Split(pair, "=") + if len(kv) == 2 { + params[kv[0]] = kv[1] + } + } + return params +} \ No newline at end of file