A tooltip of golang for parsing url query into a map.
- Add
url-query-parser
as a dependency in your program.
go get -u github.com/wangrunbo/url-query-parser
- Import it in your code:
import querypsr "github.com/wangrunbo/url-query-parser"
queryString := "a=1&b[]=2&b[]=3&c[d]=4&c[e]=5"
dataMap := querypsr.ParseString(queryString)
// dataMap = map[string]any{
// "a": "1",
// "b": []any{"2", "3"},
// "c": map[string]any{"d": "4", "e": "5"},
// }
dataMap := querypsr.Parse("a", "1")
// dataMap = map[string]any{
// "a": "1",
// }
dataMap := querypsr.Parse("a[]", "1")
// dataMap = map[string]any{
// "a": []any{"1"},
// }
dataMap := querypsr.Parse("a[b]", "1")
// dataMap = map[string]any{
// "a": map[string]any{"b": "1"},
// }
dataMap := map[string]any{
"a": "1",
"b": []any{"2"},
"c": map[string]any{"d": "3"},
}
queryString := "a=2&b[]=3&c[e]=4&f=1"
dataMap = querypsr.ParseStringToExistingMap(queryString, dataMap)
// dataMap = map[string]any{
// "a": "2",
// "b": []any{"2", "3"},
// "c": map[string]any{"d": "3", "e": "4"},
// "f": "1",
// }
dataMap := map[string]any{
"a": "1",
}
dataMap = querypsr.ParseToExistingMap("b", "2", dataMap)
// dataMap = map[string]any{
// "a": "1",
// "b": "2",
// }
dataMap := map[string]any{
"a": []any{"1"},
}
dataMap = querypsr.ParseToExistingMap("a[]", "2", dataMap)
// dataMap = map[string]any{
// "a": []any{"1", "2"},
// }
dataMap := map[string]any{
"a": map[string]any{"b": "1"},
}
dataMap = querypsr.ParseToExistingMap("a[c]", "2", dataMap)
// dataMap = map[string]any{
// "a": map[string]any{"b": "1", "c": "2"},
// }