简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM