I am trying to display a list gym classes (Yoga, Pilates etc). For each class type there are several classes, so I want to group all the Yoga classes, and all the Pilates classes and so on.
I made this function to take a slice and make a map of it
func groupClasses(classes []entities.Class) map[string][]entities.Class { classMap := make(map[string][]entities.Class) for _, class := range classes { classMap[class.ClassType.Name] = append(classMap[class.ClassType.Name], class) } return classMap }
The problem is now how can I iterate through it, according to http://golang.org/pkg/text/template/, you need to access it in .Key
format, I don't know the keys (unless I also passed a slice of keys into the template). How do I unpack this map in my view.
All I have currently is
{{ . }}
which displays something like:
map[Pilates:[{102 PILATES ~/mobifit/video/ocen.mpg 169 40 2014-05-03 23:12:12 +0000 UTC 2014-05-03 23:12:12 +0000 UTC 1899-12-30 00:00:00 +0000 UTC {PILATES Pilates 1 2014-01-22 21:46:16 +0000 UTC} {1 [email protected] password SUPERADMIN Lee Brooks {Male true} {1990-07-11 00:00:00 +0000 UTC true} {1.85 true} {88 true} 2014-01-22 21:46:16 +0000 UTC {0001-01-01 00:00:00 +0000 UTC false} {0001-01-01 00:00:00 +0000 UTC false} {0001-01-01 00:00:00 +0000 UTC false}} [{1 Mat 2014-01-22 21:46:16 +0000 UTC}]} {70 PILATES ~/mobifit/video/ocen.mpg 119 66 2014-03-31 15:12:12 +0000 UTC 2014-03-31 15:12:12 +0000 UTC 1899-12-30 00:00:00 +0000 UTC
Iterating over Map.entrySet () using For-Each loop : Map.entrySet () method returns a collection-view (Set<Map.Entry<K, V>>) of the mappings contained in this map. So we can iterate over key-value pair using getKey () and getValue () methods of Map.Entry<K, V>.
Iterating over keys or values using keySet () and values () methods Map.keySet () method returns a Set view of the keys contained in this map and Map.values () method returns a collection-view of the values contained in this map. So If you need only keys or values from the map, you can iterate over keySet or values using for-each loops.
Iterating using iterators over Map.Entry<K, V> This method is somewhat similar to first one. In first method we use for-each loop over Map.Entry<K, V>, but here we use iterators. Using iterators over Map.Entry<K, V> has it’s own advantage,i.e. we can remove entries from the map during iteration by calling iterator.remove () method.
Map.entrySet () using for each Map has a method entryset () which returns the Set object. In our case, it is Set<Entry<String, String>> and this holds Entry<String, String> objects. Now we will iterate through enhanced for loop this set. See the code below.
Check the Variables section in the Go template docs. A range may declare two variables, separated by a comma. The following should work:
{{ range $key, $value := . }} <li><strong>{{ $key }}</strong>: {{ $value }}</li> {{ end }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With