I'm just starting to learn golang and I have the following code:
https://play.golang.org/p/OBsf9MRLD8
package main
import (
"encoding/json"
"os"
)
type ResourceUsage struct {
Type string
}
type Node struct {
Resources []ResourceUsage
}
func main(){
encoder := json.NewEncoder(os.Stdout)
nodes := make([]Node, 2)
nodes[0] = Node{}
nodes[1] = Node{}
for _,n := range nodes {
n.Resources = append(n.Resources, ResourceUsage{Type: "test"})
}
encoder.Encode(nodes)
}
I was hoping it to print
[{"Resources":[{"Type:"test"}]},{"Resources":[{"Type:"test"}]}]
But instead I get:
[{"Resources":null},{"Resources":null}]
How can I accomplish the expected output?