objList := &hcl.ObjectList{} objList.Items = append(objList.Items, &hcl.Block{}) objList.Items = append(objList.Items, &hcl.Block{}) objList.Items = append(objList.Items, &hcl.Block{}) children := objList.Children() fmt.Println(len(children)) // Output: 3
block := &hcl.Block{ Type: "resource", Labels: []string{"aws_instance", "webserver"}, Body: hcl.Body{ Content: []hcl.Expr{ &hcl.Attribute{ Name: "ami", Expr: &hcl.StringLiteral{Value: "ami-0c55b159cbfafe1f0"}, }, &hcl.Attribute{ Name: "instance_type", Expr: &hcl.TemplateExpr{ Parts: []hcl.Expression{ &hcl.TemplateLiteral{Value: "t"}, &hcl.TemplateInterp{Expr: &hcl.TemplateExpr{ Parts: []hcl.Expression{ &hcl.TemplateLiteral{Value: "2"}, &hcl.TemplateLiteral{Value: ".micro"}, }, }}, }, }, }, }, }, } children := block.Body.Children() fmt.Println(len(children)) // Output: 2This code creates a Block node with a Type of "resource" and Labels of ["aws_instance", "webserver"]. It then adds two Attribute nodes to the block's Body, representing the "ami" and "instance_type" properties. The code retrieves the child nodes of the block's Body using the Children method and prints their length.