Commit 2c6716e4 authored by Boris Mühmer (ADESTIS)'s avatar Boris Mühmer (ADESTIS) 💬
Browse files

moved shapes example from main to its own directory

parent 41583d70
Loading
Loading
Loading
Loading
+2 −53
Original line number Diff line number Diff line
@@ -2,61 +2,10 @@ package main

import (
	"fmt"
	"math"
)

// Shaper ...
type Shaper interface {
	Area() float64
	Perim() float64
}

// Rect ...
type Rect struct {
	Width, Height float64
}

// Area ...
func (r Rect) Area() float64 {
	return r.Width * r.Height
}

// Perim ...
func (r Rect) Perim() float64 {
	return 2 * r.Width + 2 * r.Height
}

func (r Rect) String() string {
	return fmt.Sprintf("rect: %f * %f", r.Width, r.Height)
}

// Circle ...
type Circle struct {
	Radius float64
}

// Area ...
func (c Circle) Area() float64 {
	return math.Pi * c.Radius * c.Radius
}

// Perim ...
func (c Circle) Perim() float64 {
	return 2 * math.Pi * c.Radius
}

func (c Circle) String() string {
	return fmt.Sprintf("circle: %f", c.Radius)
}

func main() {
	shapes := []Shaper{
		Rect{Width: 4, Height: 3},
		Circle{Radius: 5},
	}
	for i, s := range shapes {
		fmt.Printf("[%d] %s: area is %f - perim is %f\n", i, s, s.Area(), s.Perim())
	}
	fmt.Println("Hello")
}

// End Of File

shapes/shapes.go

0 → 100644
+50 −0
Original line number Diff line number Diff line
package shapes

import (
	"fmt"
	"math"
)

// Shaper ...
type Shaper interface {
	Area() float64
	Perim() float64
}

// Rect ...
type Rect struct {
	Width, Height float64
}

// Area ...
func (r Rect) Area() float64 {
	return r.Width * r.Height
}

// Perim ...
func (r Rect) Perim() float64 {
	return 2 * r.Width + 2 * r.Height
}

func (r Rect) String() string {
	return fmt.Sprintf("rect: %f * %f", r.Width, r.Height)
}

// Circle ...
type Circle struct {
	Radius float64
}

// Area ...
func (c Circle) Area() float64 {
	return math.Pi * c.Radius * c.Radius
}

// Perim ...
func (c Circle) Perim() float64 {
	return 2 * math.Pi * c.Radius
}

func (c Circle) String() string {
	return fmt.Sprintf("circle: %f", c.Radius)
}

shapes/shapes_test.go

0 → 100644
+15 −0
Original line number Diff line number Diff line
package shapes

import (
	"testing"
)

func TestShapes(t *testing.T){
	shapes := []Shaper{
		Rect{Width: 4, Height: 3},
		Circle{Radius: 5},
	}
	for i, s := range shapes {
		t.Logf("[%d] %s: area is %f - perim is %f\n", i, s, s.Area(), s.Perim())
	}
}
 No newline at end of file