Skip to content

Instantly share code, notes, and snippets.

@muhlemmer
Last active February 15, 2019 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhlemmer/43a513f5872955f13de6993c154ca280 to your computer and use it in GitHub Desktop.
Save muhlemmer/43a513f5872955f13de6993c154ca280 to your computer and use it in GitHub Desktop.
Go map patterns
package main
import (
"fmt"
"sort"
)
type myIntMap map[int]string
func (m myIntMap) sort() (index []int) {
for k, _ := range m {
index = append(index, k)
}
sort.Ints(index)
return
}
type myStringMap map[string]string
func (m myStringMap) sort() (index []string) {
for k, _ := range m {
index = append(index, k)
}
sort.Strings(index)
return
}
func main() {
mim := myIntMap{
1: "one",
11: "eleven",
3: "three",
}
for _, k := range mim.sort() {
fmt.Println(mim[k])
}
msm := myStringMap{
"b": "two",
"s": "twenty",
"e": "five",
}
for _, k := range msm.sort() {
fmt.Println(msm[k])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment