Skip to content

Instantly share code, notes, and snippets.

@chaudharisuresh997
Created February 17, 2020 08:44
Show Gist options
  • Save chaudharisuresh997/1b2159c8034440b0702b585c6dbc331e to your computer and use it in GitHub Desktop.
Save chaudharisuresh997/1b2159c8034440b0702b585c6dbc331e to your computer and use it in GitHub Desktop.
Nested Map golang tutorial
package main
import (
"fmt"
"encoding/json"
)
type ProjectAttributes struct{
ProjectId string
Attributes map[string]interface{}
}
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]interface{}
mp:=make(map[string]interface{})//map[string]string)
//mp["attributes"]=make(map[string]string)
mp["phase"]=phase
mp["budget"]="40"
//Add Phases to Attributes
//mp=phase//string(phasesAsString)
//mp["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.Printf("Json %v",string(bt))
pt:=ProjectAttributes{}
if e:=json.Unmarshal(bt,&pt);e!=nil{
fmt.Printf("ERR %v",e.Error())
}
fmt.Printf("Obj %v",pt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment