Guest User

Untitled

a guest
Apr 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "log"
  5. )
  6.  
  7. // Make a new ToDo type that is a typed collection of fields
  8. // (Title and Status), both of which are of type string
  9. type ToDo struct {
  10. Title, Status string
  11. }
  12.  
  13. // Declare variable 'todoSlice' that is a slice made up of
  14. // type ToDo items
  15. var todoSlice []ToDo
  16.  
  17. // GetToDo takes a string type and returns a ToDo
  18. func GetToDo(title string) ToDo {
  19. var found ToDo
  20. // Range statement that iterates over todoArray
  21. // 'v' is the value of the current iterateee
  22. for _, v := range todoSlice {
  23. if v.Title == title {
  24. found = v
  25. }
  26. }
  27. // found will either be the found ToDo or a zerod ToDo
  28. return found
  29. }
  30.  
  31. // MakeToDo takes a ToDo type and appends to the todoArray
  32. func MakeToDo(todo ToDo) ToDo {
  33. todoSlice = append(todoSlice, todo)
  34. return todo
  35. }
  36.  
  37. // EditToDo takes a string type and a ToDo type and edits an item in the todoArray
  38. func EditToDo(title string, editToDo ToDo) ToDo {
  39. var edited ToDo
  40. // 'i' is the index in the array and 'v' the value
  41. for i, v := range todoSlice {
  42. if v.Title == title {
  43. todoSlice[i] = editToDo
  44. edited = editToDo
  45. }
  46. }
  47. // edited will be the edited ToDo or a zeroed ToDo
  48. return edited
  49. }
  50.  
  51. // DeleteToDo takes a ToDo type and deletes it from todoArray
  52. func DeleteToDo(todo ToDo) ToDo {
  53. var deleted ToDo
  54. for i, v := range todoSlice {
  55. if v.Title == todo.Title && v.Status == todo.Status {
  56. // Delete ToDo by appending the items before it and those
  57. // after to the todoArray variable
  58. todoSlice = append(todoSlice[:i], todoSlice[i+1:]...)
  59. deleted = todo
  60. break
  61. }
  62. }
  63. return deleted
  64. }
  65.  
  66. func main() {
  67. log.Println("1. todo Slice: ", todoSlice)
  68. finishApp := ToDo{"Finish App", "Started"}
  69. makeDinner := ToDo{"Make Dinner", "Not Started"}
  70. walkDog := ToDo{"Walk the dog", "Not Started"}
  71. MakeToDo(finishApp)
  72. MakeToDo(makeDinner)
  73. MakeToDo(walkDog)
  74. log.Println("2. todo Slice: ", todoSlice)
  75. DeleteToDo(makeDinner)
  76. log.Println("3. todo Slice: ", todoSlice)
  77. MakeToDo(makeDinner)
  78. log.Println("4. todo Slice: ", todoSlice)
  79. log.Println("5.", GetToDo("Finish App"))
  80. log.Println("6.", GetToDo("Finish Application"))
  81. EditToDo("Finish App", ToDo{"Finish App", "Completed"})
  82. log.Println("7. todo Slice: ", todoSlice)
  83. }
Add Comment
Please, Sign In to add comment