I have an interface
type Shape interface {
area() float32
circumference() float32
}
I want to create different shapes such as a circle and a rectangle, for the circle I need to know the radius, and for the rectangle the 2 sides. So the code for each is like the following:
type DataCircle struct {
radius float
}
(*DataCircle) area() float32 {
return 3.14 * DataCircle.radius * DataCircle.radius;
}
(*DataCircle) circumference() float32 {
return 2 * 3.14 * DataCircle.radius;
}
Similarly we have code for a rectangle which implement the interface Shape, with the following struct
type DataRectangle struct {
side1 float
side2 float
}
I want to create many different Rectangles and many different Circles, each one has different radius/sides. In the end I want to put them in a single array and be able to do something like the following
for _, shape := range all_shapes_in_array {
fmt.Printf("%f %f", shape.area(), shape.circumference())
}
In a normal object oriented language this is pretty straight forward, but how do I do it in Golang?