Exercises & activities from the go workshop provided by Packt: https://courses.packtpub.com/courses/go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. )
  7. type locale struct {
  8. language string
  9. region string
  10. }
  11. func getLocales() map[locale]struct{} {
  12. supportedLocales := make(map[locale]struct{}, 5)
  13. supportedLocales[locale{"en", "US"}] = struct{}{}
  14. supportedLocales[locale{"en", "CN"}] = struct{}{}
  15. supportedLocales[locale{"fr", "CN"}] = struct{}{}
  16. supportedLocales[locale{"fr", "FR"}] = struct{}{}
  17. supportedLocales[locale{"ru", "RU"}] = struct{}{}
  18. return supportedLocales
  19. }
  20. func localExists(l locale) bool {
  21. _, exists := getLocales()[l]
  22. return exists
  23. }
  24. func main() {
  25. if len(os.Args) != 2 {
  26. fmt.Println("No locale passed")
  27. os.Exit(1)
  28. }
  29. localeParts := strings.Split(os.Args[1], "_")
  30. if len(localeParts) != 2 {
  31. fmt.Printf("Invalid locale passed: %v\n", os.Args[1])
  32. os.Exit(1)
  33. }
  34. passedLocale := locale{
  35. language: localeParts[0],
  36. region: localeParts[1],
  37. }
  38. if !localExists(passedLocale) {
  39. fmt.Printf("Locale not supported: %v\n", os.Args[1])
  40. os.Exit(1)
  41. }
  42. fmt.Println("Locale passed is supported")
  43. }