Last active
February 15, 2019 22:52
-
-
Save muhlemmer/43a513f5872955f13de6993c154ca280 to your computer and use it in GitHub Desktop.
Go map patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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