shapes: add Circle struct and Area func

This commit is contained in:
vinchent 2024-09-11 09:06:46 +02:00
parent d5d910db5a
commit ff89c50710
2 changed files with 19 additions and 9 deletions

View File

@ -1,10 +1,16 @@
package shapes
import "math"
type Rectangle struct {
Width float64
Height float64
}
type Circle struct {
Radius float64
}
func (r Rectangle) Perimeter() float64 {
return 2 * (r.Width + r.Height)
}
@ -12,3 +18,7 @@ func (r Rectangle) Perimeter() float64 {
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
func (c Circle) Area() float64 {
return c.Radius * c.Radius * math.Pi
}

View File

@ -23,13 +23,13 @@ func TestArea(t *testing.T) {
}
})
// t.Run("circles", func(t *testing.T) {
// circle := Circle{10}
// got := Area(circle)
// exp := 314.1592653589793
//
// if got != exp {
// t.Errorf("got %.2f expected %.2f", got, exp)
// }
// })
t.Run("circles", func(t *testing.T) {
circle := Circle{10}
got := circle.Area()
exp := 314.1592653589793
if got != exp {
t.Errorf("got %.2f expected %.2f", got, exp)
}
})
}