map_drinks.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // map_drinks.go
  2. package main
  3. import (
  4. "fmt"
  5. "sort"
  6. )
  7. func main() {
  8. drinks := map[string]string{
  9. "beer": "bière",
  10. "wine": "vin",
  11. "water": "eau",
  12. "coffee": "café",
  13. "thea": "thé"}
  14. sdrinks := make([]string, len(drinks))
  15. ix := 0
  16. fmt.Printf("The following drinks are available:\n")
  17. for eng := range drinks {
  18. sdrinks[ix] = eng
  19. ix++
  20. fmt.Println(eng)
  21. }
  22. fmt.Println("")
  23. for eng, fr := range drinks {
  24. fmt.Printf("The french for %s is %s\n", eng, fr)
  25. }
  26. // SORTING:
  27. fmt.Println("")
  28. fmt.Println("Now the sorted output:")
  29. sort.Strings(sdrinks)
  30. fmt.Printf("The following sorted drinks are available:\n")
  31. for _, eng := range sdrinks {
  32. fmt.Println(eng)
  33. }
  34. fmt.Println("")
  35. for _, eng := range sdrinks {
  36. fmt.Printf("The french for %s is %s\n", eng, drinks[eng])
  37. }
  38. }
  39. /* Output:
  40. The following drinks are available:
  41. wine
  42. beer
  43. water
  44. coffee
  45. thea
  46. The french for wine is vin
  47. The french for beer is bière
  48. The french for water is eau
  49. The french for coffee is café
  50. The french for thea is thé
  51. Now the sorted output:
  52. The following sorted drinks are available:
  53. beer
  54. coffee
  55. thea
  56. water
  57. wine
  58. The french for beer is bière
  59. The french for coffee is café
  60. The french for thea is thé
  61. The french for water is eau
  62. The french for wine is vin
  63. */