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.

47 lines
694 B

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func getUsers() map[string]string {
  7. return map[string]string{
  8. "305": "Sue",
  9. "204": "Bob",
  10. "631": "Jake",
  11. "073": "Tracy",
  12. }
  13. }
  14. func getUser(id string) (string, bool) {
  15. users := getUsers()
  16. user, exists := users[id]
  17. return user, exists
  18. }
  19. func main() {
  20. if len(os.Args) != 2 {
  21. fmt.Println("User ID not passed")
  22. os.Exit(1)
  23. }
  24. userID := os.Args[1]
  25. name, exists := getUser(userID)
  26. if !exists {
  27. fmt.Printf("Passed user ID (%v) not found.\nUsers: \n", userID)
  28. for key, value := range getUsers() {
  29. fmt.Println(" ID:", key, "Name:", value)
  30. }
  31. os.Exit(1)
  32. }
  33. fmt.Println("Name:", name)
  34. }