Get a query param by name without any dynamic allocations. For small numbers of query params, it is faster to call this method multiple times than to use `url.ParseQuery` and then call `Get` on the resulting `url.Values`. This method does not support multiple values for the same key, so using `val=1
(query, name string)
| 22 | // This method does not support multiple values for the same key, so using |
| 23 | // `val=1,2,3` is preferable to `val=1&val=2&val=3`. |
| 24 | func Get(query, name string) string { |
| 25 | pos := 0 |
| 26 | for pos < len(query) { |
| 27 | end := strings.IndexRune(query[pos:], '=') |
| 28 | if end == -1 { |
| 29 | end = strings.IndexRune(query[pos:], '&') |
| 30 | } |
| 31 | |
| 32 | if end == -1 { |
| 33 | end = len(query) |
| 34 | } else { |
| 35 | end += pos |
| 36 | } |
| 37 | |
| 38 | ueName, _ := url.QueryUnescape(query[pos:end]) |
| 39 | if ueName == name { |
| 40 | if end == len(query) || query[end] == '&' { |
| 41 | return "true" |
| 42 | } |
| 43 | pos = end + 1 |
| 44 | end = strings.IndexRune(query[pos:], '&') |
| 45 | if end == -1 { |
| 46 | end = len(query) |
| 47 | } else { |
| 48 | end += pos |
| 49 | } |
| 50 | escaped, _ := url.QueryUnescape(query[pos:end]) |
| 51 | return escaped |
| 52 | } |
| 53 | tmp := pos |
| 54 | pos = strings.IndexRune(query[pos:], '&') |
| 55 | if pos == -1 { |
| 56 | break |
| 57 | } |
| 58 | pos += tmp + 1 |
| 59 | } |
| 60 | return "" |
| 61 | } |
no outgoing calls