0

There is a format string like this:

"key1=value1&key2=value2"

How to convert this string to a map elegantly:

{"key1":"value1","key2":"value2"}

Is there any good utils like Guava's MapSplitter?

3
  • 2
    Are you trying to parse the query string in a URL? Commented Dec 15, 2022 at 4:07
  • not a url, but like that Commented Dec 15, 2022 at 6:23
  • do you want to parse the keys as string and the values as typed value? Like, k1=5&k2=five => {"k1": 5, "k2": "five"} or {"k1": "5", "k2": "five"}? Commented Dec 15, 2022 at 6:49

1 Answer 1

2

You can use strings.Split() function twice to split the entire string into a key=value pairs by & and then again to split each pair to key and value by =.

Quick playground without handling corner cases: https://go.dev/play/p/t8oMbA72GCB

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.