Jerry Zhou
Jerry Zhou

Reputation: 229

How to convert key-value format string to map in golang?

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?

Upvotes: 0

Views: 674

Answers (1)

blami
blami

Reputation: 7431

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

Upvotes: 2

Related Questions