Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chaudharisuresh997/ea05f5b03b2293c58a98d22d0656f5d7 to your computer and use it in GitHub Desktop.
Save chaudharisuresh997/ea05f5b03b2293c58a98d22d0656f5d7 to your computer and use it in GitHub Desktop.
Nested Map Golang
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