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.

55 lines
875 B

  1. package main
  2. import "fmt"
  3. type Employee struct {
  4. Id int
  5. FirstName string
  6. LastName string
  7. }
  8. type Developer struct {
  9. Individual Employee
  10. HourlyRate int
  11. WorkWeek [7]int
  12. }
  13. type Weekday int
  14. const (
  15. Sunday Weekday = iota
  16. Monday
  17. Tuesday
  18. Wednesday
  19. Thursday
  20. Friday
  21. Saturday
  22. )
  23. func main() {
  24. d := Developer{Individual: Employee{Id: 1, FirstName: "Tony", LastName: "Stark"}, HourlyRate: 10}
  25. d.LogHours(Monday, 8)
  26. d.LogHours(Tuesday, 10)
  27. fmt.Println("Hours worked on monday:", d.WorkWeek[Monday])
  28. fmt.Println("Hours worked on tuesday:", d.WorkWeek[Tuesday])
  29. fmt.Printf("Hours worked this week: %d\n", d.HoursWorked())
  30. }
  31. func (d *Developer) LogHours(day Weekday, hours int) {
  32. d.WorkWeek[day] = hours
  33. }
  34. func (d *Developer) HoursWorked() int {
  35. total := 0
  36. for _, v := range d.WorkWeek {
  37. total += v
  38. }
  39. return total
  40. }