Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert byte array to map[string,string] in golang

I want to convert byte array to map[string,string] using golang. I tried this:

var byte := json.Marshal(input)
var map := make(map[string]string *byte) // NOT WORKING

if byte holds value like {\"hello\":\"world\",...} How to create the map from byte array

Please help.

like image 602
Poppy Avatar asked Jun 02 '16 10:06

Poppy


People also ask

How do I create a string string map in Golang?

Create a map in Golang[string] - indicates that keys of the map are of string type. float32 - indicates that values of the map are of float type. {Golang", "Java", "Python"} - keys of the map. {85, 80, 81} - values of the map.

What is a [] byte in Golang?

The byte type in Golang is an alias for the unsigned integer 8 type ( uint8 ). The byte type is only used to semantically distinguish between an unsigned integer 8 and a byte. The range of a byte is 0 to 255 (same as uint8 ).

What does map string interface{} mean?

map[string]interface{} is a map whose keys are strings and values are any type.

What is a byte string in Golang?

Byte. A byte in Go is an unsigned 8-bit integer. That means it has a limit of 0–255 in the numerical range. type byte = uint8. According to Go documentation, Byte is an alias for uint8 and is the same as uint8 in all ways.


1 Answers

You probably want to do something like

m := make(map[string]string)
err := json.Unmarshal(input, &m)

This creates a new map[string]string and unmarshals a byte array into it.

like image 195
plusmid Avatar answered Nov 15 '22 05:11

plusmid