Add structs package
This commit is contained in:
		
							
								
								
									
										37
									
								
								structs/shapes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								structs/shapes.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | |||||||
|  | package structs | ||||||
|  |  | ||||||
|  | import "math" | ||||||
|  |  | ||||||
|  | type Shape interface { | ||||||
|  | 	Area() float64 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type Rectangle struct { | ||||||
|  | 	Width  float64 | ||||||
|  | 	Height float64 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type Circle struct { | ||||||
|  | 	Radius float64 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type Triangle struct { | ||||||
|  | 	Base   float64 | ||||||
|  | 	Height float64 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func Perimiter(r Rectangle) float64 { | ||||||
|  | 	return 2 * (r.Width + r.Height) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (c Circle) Area() float64 { | ||||||
|  | 	return math.Pi * c.Radius * c.Radius | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r Rectangle) Area() float64 { | ||||||
|  | 	return r.Width * r.Height | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (t Triangle) Area() float64 { | ||||||
|  | 	return t.Base * t.Height / 2 | ||||||
|  | } | ||||||
							
								
								
									
										38
									
								
								structs/shapes_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								structs/shapes_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | |||||||
|  | package structs | ||||||
|  |  | ||||||
|  | import "testing" | ||||||
|  |  | ||||||
|  | func TestPerimiter(t *testing.T) { | ||||||
|  | 	rectangle := Rectangle{10.0, 10.0} | ||||||
|  | 	got := Perimiter(rectangle) | ||||||
|  | 	want := 40.0 | ||||||
|  |  | ||||||
|  | 	if got != want { | ||||||
|  | 		t.Errorf("got %.2f want %.2f", got, want) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestArea(t *testing.T) { | ||||||
|  |  | ||||||
|  | 	areaTests := []struct { | ||||||
|  | 		name    string | ||||||
|  | 		shape   Shape | ||||||
|  | 		hasArea float64 | ||||||
|  | 	}{ | ||||||
|  | 		{name: "Rectangle", shape: Rectangle{Width: 12, Height: 6}, hasArea: 72.0}, | ||||||
|  | 		{name: "Circle", shape: Circle{Radius: 10}, hasArea: 314.1592653589793}, | ||||||
|  | 		{name: "Triangle", shape: Triangle{Base: 12, Height: 6}, hasArea: 36.0}, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, tt := range areaTests { | ||||||
|  | 		// using tt.name from the case to use it as the `t.Run` test name | ||||||
|  | 		t.Run(tt.name, func(t *testing.T) { | ||||||
|  | 			got := tt.shape.Area() | ||||||
|  | 			if got != tt.hasArea { | ||||||
|  | 				t.Errorf("%#v got %g want %g", tt.shape, got, tt.hasArea) | ||||||
|  | 			} | ||||||
|  | 		}) | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user