Created
July 17, 2013 17:02
-
-
Save ekanna/6022426 to your computer and use it in GitHub Desktop.
Generating arbitrary JSON data using map in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generating arbitrary JSON data using map | |
package main | |
import "fmt" | |
import "encoding/json" | |
func main() { | |
keys := []string{"Segment", "Jan", "Feb", "Apr"} | |
values := []interface{}{"ME",1000,2000,3000} | |
fmt.Println(keys) | |
fmt.Println(values) | |
// map values to keys | |
m := make(map[string]interface{}) | |
for i,v := range values { | |
m[keys[i]] = v | |
} | |
fmt.Println(m) | |
// convert map to JSON | |
data, _ := json.Marshal(m) | |
fmt.Printf("%s", data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment