Skip to content

Instantly share code, notes, and snippets.

@ealipio
Created August 11, 2019 01:20
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 ealipio/b4e82a14327f2aeb3a9d95921b044aa1 to your computer and use it in GitHub Desktop.
Save ealipio/b4e82a14327f2aeb3a9d95921b044aa1 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sort"
)
type user struct {
name string
surname string
}
func main() {
//using literal construction for maps
// declare and initialize the map with values:
users := map[string]user{
"Alipio": {"Eisson", "Alipio"},
"Mickey": {"Mickey", "Mouse"},
"Peter": {"Peter", "Parker"},
"Bruce": {"Bruce", "Wayne"},
}
for key, value := range users {
fmt.Println(key, value)
}
fmt.Println()
for key := range users {
fmt.Println(key)
}
fmt.Println("====================")
// pull the keys from the map
var keys []string
for key := range users {
keys = append(keys, key)
}
fmt.Println(keys) //[Mickey Peter Bruce Alipio]
// sorting keys alphabeticall
sort.Strings(keys)
fmt.Println(keys) // [Alipio Bruce Mickey Peter]
for _, key := range keys {
fmt.Println(key, users[key])
}
fmt.Println("====================")
// delete the key Mickey
delete(users, "Mickey")
// find the deleted key
u, found := users["Mickey"]
fmt.Println(u, found) // { } false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment