Created
September 19, 2019 09:53
-
-
Save chaudharisuresh997/ea05f5b03b2293c58a98d22d0656f5d7 to your computer and use it in GitHub Desktop.
Nested Map 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
package main | |
import ( | |
"fmt" | |
"encoding/json" | |
) | |
type ProjectAttributes struct{ | |
ProjectId string | |
Attributes map[string]map[string]string | |
} | |
func main() { | |
fmt.Println("Hello, playground") | |
phase:=make(map[string]string) | |
phase["p1"]="60" | |
phase["p2"]="60" | |
//convert phases to json string | |
phasesAsString,_:=json.Marshal(phase) | |
if(phasesAsString==nil){ | |
fmt.Println("phasesAsString nil") | |
} | |
//Attributes started | |
var mp=map[string]map[string]string{} | |
mp=make(map[string]map[string]string) | |
mp["attributes"]=make(map[string]string) | |
//Add Phases to Attributes | |
mp["attributes"]["Phases"]=string(phasesAsString) | |
mp["attributes"]["budget"]="20" | |
//Make custom struct | |
var projectAttributes ProjectAttributes | |
projectAttributes.ProjectId="1" | |
projectAttributes.Attributes=mp | |
bt,_:=json.Marshal(projectAttributes) | |
if(bt==nil){ | |
fmt.Println("bt nil") | |
} | |
fmt.Println(string(bt)) | |
var dst ProjectAttributes | |
dst.Attributes=make(map[string]map[string]string) | |
encoded:={"projectId":"1","attributes":{"attributes":{"Phases":"{\"p1\":\"60\",\"p2\":\"60\"}","budget":"20"}}} | |
e:=json.Unmarshal(bt,&dst) | |
if(e!=nil){ | |
fmt.Printf("%v",e) | |
} | |
if(&dst!=nil){ | |
fmt.Printf("%v",dst) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment