Skip to content

Instantly share code, notes, and snippets.

@lbragstad
Last active February 6, 2019 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbragstad/99e826870a3305389db85985cee5ba9d to your computer and use it in GitHub Desktop.
Save lbragstad/99e826870a3305389db85985cee5ba9d to your computer and use it in GitHub Desktop.
A simple exercise in go

Shapes

You need to use structs to model triangles and squares. Your module should have a printArea() that takes a shape as a receiver. The printArea() function should call an interface (e.g., getArea) that is implemented by your shapes and contains the implementation for calculating the area for those shapes. Shapes cannot have negative values.

Tests

Use the included tests to exercise your implementation. The tests assume your implementation is in main.go. To run the tests:

$ go test main_test.go

Hints

  • Create a public struct called Triangle that has a base field and a height field
  • Create a public struct called Square that has a sideLength field
  • Use a factory to create new shapes, validate input, and handle errors
  • Factory methods should return values
  • Create an interface called shape that defines a getArea() function that returns a value of float64
  • Implement getArea() for both triangle and square types
  • Implement a printArea() function that relies on the shape interface to print the area of the shape to stdout
package main
import "fmt"
func main() {
fmt.Println("vim-go")
}
package main
import (
"errors"
"fmt"
"log"
)
// Triangle is a struct for modeling triangle shapes
type Triangle struct {
base, height float64
}
// Square is a struct for modeling a square shapes
type Square struct {
sideLength float64
}
type shape interface {
getArea() float64
}
func (t Triangle) getArea() float64 {
return t.base * t.height / 2
}
func (s Square) getArea() float64 {
return s.sideLength * s.sideLength
}
func printArea(s shape) {
fmt.Println("Area:", s.getArea())
}
// NewSquare returns a new Square struct.
func NewSquare(sl float64) (Square, error) {
if sl < 0 {
var s Square
return s, errors.New("squares cannot have negative values")
}
return Square{sideLength: sl}, nil
}
// NewTriangle returns a new Triangle struct.
func NewTriangle(b float64, h float64) (Triangle, error) {
if b < 0 || h < 0 {
var t Triangle
return t, errors.New("triangles cannot have negative values")
}
return Triangle{base: b, height: h}, nil
}
func handleErr(err error) {
if err != nil {
log.Fatal("Error: ", err)
}
}
func main() {
s, err := NewSquare(4)
handleErr(err)
t, err := NewTriangle(25, 8)
handleErr(err)
printArea(s)
printArea(t)
}
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSquareGetArea(t *testing.T) {
sq := Square{sideLength: 5}
a := sq.getArea()
expected := float64(25)
fm := fmt.Sprintf("Expected area of square to be %f, but got %f instead", expected, a)
assert.Equal(t, expected, a, fm)
}
func TestSquareGetAreaWithFloats(t *testing.T) {
sq := Square{sideLength: 5.5}
a := sq.getArea()
expected := float64(30.25)
fm := fmt.Sprintf("Expected area of square to be %f, but got %f instead", expected, a)
assert.Equal(t, expected, a, fm)
}
func TestTriangleGetArea(t *testing.T) {
tri := Triangle{base: 5, height: 10}
a := tri.getArea()
expected := float64(25)
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a)
assert.Equal(t, expected, a, fm)
}
func TestTriangleGetAreaWithFloats(t *testing.T) {
tri := Triangle{base: 5.2, height: 10.2}
a := tri.getArea()
expected := float64(26.52)
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a)
assert.Equal(t, expected, a, fm)
}
func TestNewSquareAcceptsIntegers(t *testing.T) {
sq, _ := NewSquare(2)
a := sq.getArea()
expected := float64(4)
fm := fmt.Sprintf("Expected area of square to be %f, but got %f instead", expected, a)
assert.Equal(t, expected, a, fm)
}
func TestNewSquareAcceptsFloats(t *testing.T) {
sq, _ := NewSquare(5.5)
a := sq.getArea()
expected := float64(30.25)
fm := fmt.Sprintf("Expected area of square to be %f, but got %f instead", expected, a)
assert.Equal(t, expected, a, fm)
}
func TestNewSquareDoesNotAcceptNegativeValues(t *testing.T) {
_, err := NewSquare(-2)
expected := "squares cannot have negative values"
assert.Equal(t, expected, err.Error(), "Unexpected exception")
}
func TestNewTriangleAcceptsBaseInteger(t *testing.T) {
tri, _ := NewTriangle(5, 10.5)
a := tri.getArea()
expected := float64(26.25)
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a)
assert.Equal(t, expected, a, fm)
}
func TestNewTriangleAcceptsHeightInteger(t *testing.T) {
tri, _ := NewTriangle(5.5, 10)
a := tri.getArea()
expected := float64(27.5)
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a)
assert.Equal(t, expected, a, fm)
}
func TestNewTriangleAcceptsFloats(t *testing.T) {
tri, _ := NewTriangle(5.5, 10.5)
a := tri.getArea()
expected := float64(28.875)
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a)
assert.Equal(t, expected, a, fm)
}
func TestNewTriangleDoesNotAcceptNegativeBaseValues(t *testing.T) {
_, err := NewTriangle(-2, 5)
expected := "triangles cannot have negative values"
assert.Equal(t, expected, err.Error(), "Unexpected exception")
}
func TestNewTriangleDoesNotAcceptNegativeHeightValues(t *testing.T) {
_, err := NewTriangle(2, -5)
expected := "triangles cannot have negative values"
assert.Equal(t, expected, err.Error(), "Unexpected exception")
}
func TestNewTriangleDoesNotAcceptNegativeValues(t *testing.T) {
_, err := NewTriangle(-2, -5)
expected := "triangles cannot have negative values"
assert.Equal(t, expected, err.Error(), "Unexpected exception")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment