Skip to content

Instantly share code, notes, and snippets.

@aquilax
Created July 25, 2011 11:02
Show Gist options
  • Save aquilax/1103910 to your computer and use it in GitHub Desktop.
Save aquilax/1103910 to your computer and use it in GitHub Desktop.
NodeList in GO
package main
import (
"fmt"
"strconv"
)
type Elements map[string]float32
type Node struct {
name string
elements Elements
}
type NodeList map[string]*Node
func (nl *NodeList) Add(node *Node) {
(*nl)[(*node).name] = node
}
func (nl *NodeList) Fill() {
for i := 0; i < 10; i++ {
var node Node
node.elements = make(Elements)
node.name = "node" + fmt.Sprintf("%d", i)
for x := 1; x < 5; x++ {
node.elements["el"+strconv.Itoa(x)] = float32(x)
}
nl.Add(&node)
}
}
func (nl *NodeList) Print() {
for _, node := range *(nl) {
fmt.Println(node.name)
for name, val := range node.elements {
fmt.Printf("\t%s:%f\n", name, val)
}
}
}
func main() {
mylist := make(NodeList)
mylist.Fill()
mylist.Print()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment