With PHP and Javascript (and Node) parsing JSON is a very trivial operation. From the looks of it Go is rather more complicated. Consider the example below
package main
import ("encoding/json";"fmt")
type fileData struct{
tn string
size int
}
type jMapA map[string] string
type jMapB map[string] fileData
func parseMapA(){
var dat jMapA
s := `{"lang":"Node","compiled":"N","fast":"maybe"}`
if err := json.Unmarshal([]byte(s), &dat); err != nil {
panic(err)
}
fmt.Println(dat);
for k,v := range dat{
fmt.Println(k,v)
}
}
func parseMapB(){
var dat jMapB
s := `{"f1":{"tn":"F1","size":1024},"f2":{"tn":"F2","size":2048}}`
if err := json.Unmarshal([]byte(s), &dat); err != nil {
panic(err)
}
fmt.Println(dat);
for k,v := range dat{
fmt.Println(k,v)
}
}
func main() {
parseMapA()
parseMapB()
}
The parseMapA()
call obligingly returns
map[lang:Node Compiled:N fast:maybe]
lang Node
compiled N
fast maybe
However, parseMapB()
returns
map[f1:{ 0}, f2:{ 0}]
f2 { 0}
f1 { 0}
I am into my first few hours with Go so I imagine I am doing something wrong here. However, I have no idea what that might be. I'd much appreciate any help. More generally, what would the Go equivalent of the Node code
for(p in obj){
doSomethingWith(obj[p]);
}
be in Go?