Created
February 17, 2020 08:44
-
-
Save chaudharisuresh997/1b2159c8034440b0702b585c6dbc331e to your computer and use it in GitHub Desktop.
Nested Map golang tutorial
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]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