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.

62 lines
884 B

  1. package main
  2. import "fmt"
  3. type Shape interface {
  4. Area() float64
  5. Name() string
  6. }
  7. type triangle struct {
  8. base float64
  9. height float64
  10. }
  11. type rectangle struct {
  12. length float64
  13. width float64
  14. }
  15. type square struct {
  16. side float64
  17. }
  18. func (t triangle) Area() float64 {
  19. return (t.base * t.height) / 2
  20. }
  21. func (t triangle) Name() string {
  22. return "triangle"
  23. }
  24. func (r rectangle) Area() float64 {
  25. return r.length * r.width
  26. }
  27. func (r rectangle) Name() string {
  28. return "rectangle"
  29. }
  30. func (s square) Area() float64 {
  31. return s.side * s.side
  32. }
  33. func (s square) Name() string {
  34. return "square"
  35. }
  36. func printShapeDetails(shapes ...Shape) {
  37. for _, item := range shapes {
  38. fmt.Printf("The area of %s is %.2f\n", item.Name(), item.Area())
  39. }
  40. }
  41. func main() {
  42. t := triangle{15.5, 20.1}
  43. r := rectangle{20, 10}
  44. s := square{10}
  45. printShapeDetails(t, r, s)
  46. }