diff --git a/.travis.yml b/.travis.yml index b5e8821aa..c4fd81735 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,8 @@ go: install: true -script: go test ./... \ No newline at end of file +before_script: + - go get github.com/golang/lint/golint + +script: + - ./build.sh \ No newline at end of file diff --git a/arrays/v6/sum.go b/arrays/v6/sum.go index d74bf0fb2..9e5dd6341 100644 --- a/arrays/v6/sum.go +++ b/arrays/v6/sum.go @@ -9,7 +9,7 @@ func Sum(numbers []int) int { return sum } -// SumAll calculates the respective sums of every slice passed in +// SumAllTails calculates the respective sums of every slice passed in func SumAllTails(numbersToSum ...[]int) []int { var sums []int for _, numbers := range numbersToSum { diff --git a/build.sh b/build.sh index c140e25b4..fade6309f 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash +set -e + go test ./... go vet ./... go fmt ./... -# golint ./... +golint ./... diff --git a/di/v2/di.go b/di/v2/di.go index b1ce111fd..6bd22d174 100644 --- a/di/v2/di.go +++ b/di/v2/di.go @@ -11,7 +11,7 @@ func Greet(writer io.Writer, name string) { fmt.Fprintf(writer, "Hello, %s", name) } -// MyGreetingHandler says Hello, world over HTTP +// MyGreeterHandler says Hello, world over HTTP func MyGreeterHandler(w http.ResponseWriter, r *http.Request) { Greet(w, "world") } diff --git a/mocking/v3/main.go b/mocking/v3/main.go index 3bbd1ece4..78cbd9d47 100644 --- a/mocking/v3/main.go +++ b/mocking/v3/main.go @@ -14,11 +14,12 @@ type Sleeper interface { // ConfigurableSleeper is an implementation of Sleeper with a defined delay type ConfigurableSleeper struct { - duration time.Duration + Duration time.Duration } +// Sleep will pause execution for the defined Duration func (o *ConfigurableSleeper) Sleep() { - time.Sleep(o.duration) + time.Sleep(o.Duration) } const finalWord = "Go!" diff --git a/pointers-and-errors.md b/pointers-and-errors.md index bcafa8e05..60b796b68 100644 --- a/pointers-and-errors.md +++ b/pointers-and-errors.md @@ -484,12 +484,12 @@ It would be really annoying for the test to fail if someone wanted to re-word th In Go, errors are values, so we can refactor it out into a variable and have a single source of truth for it. ```go -var InsufficientFundsError = errors.New("cannot withdraw, insufficient funds") +var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds") func (w *Wallet) Withdraw(amount Bitcoin) error { if amount > w.balance { - return InsufficientFundsError + return ErrInsufficientFunds } w.balance -= amount @@ -523,7 +523,7 @@ func TestWallet(t *testing.T) { err := wallet.Withdraw(Bitcoin(100)) assertBalance(t, wallet, Bitcoin(20)) - assertError(t, err, InsufficientFundsError) + assertError(t, err, ErrInsufficientFunds) }) } @@ -550,7 +550,7 @@ And now the test is easier to follow too. I have moved the helpers out of the main test function just so when someone opens up a file they can start reading our assertions first, rather than some helpers. -Another useful property of tests is that they help us understand the _real_ usage of our code so we can make sympathetic code. We can see here that a developer can simply call our code and do an equals check to `InsufficientFundsError` and act accordingly. +Another useful property of tests is that they help us understand the _real_ usage of our code so we can make sympathetic code. We can see here that a developer can simply call our code and do an equals check to `ErrInsufficientFunds` and act accordingly. ### Unchecked errors @@ -593,7 +593,7 @@ func TestWallet(t *testing.T) { err := wallet.Withdraw(Bitcoin(100)) assertBalance(t, wallet, Bitcoin(20)) - assertError(t, err, InsufficientFundsError) + assertError(t, err, ErrInsufficientFunds) }) } diff --git a/pointers/v1/wallet.go b/pointers/v1/wallet.go index dbd93f69c..44b3199fe 100644 --- a/pointers/v1/wallet.go +++ b/pointers/v1/wallet.go @@ -2,20 +2,24 @@ package main import "fmt" +// Bitcoin represents a number of Bitcoins type Bitcoin int func (b Bitcoin) String() string { return fmt.Sprintf("%d BTC", b) } +// Wallet stores the number of Bitcoin someone owns type Wallet struct { balance Bitcoin } +// Deposit will add some Bitcoin to a wallet func (w *Wallet) Deposit(amount Bitcoin) { w.balance += amount } +// Balance returns the number of Bitcoin a wallet has func (w *Wallet) Balance() Bitcoin { return w.balance } diff --git a/pointers/v2/wallet.go b/pointers/v2/wallet.go index 5a4557392..81cc897db 100644 --- a/pointers/v2/wallet.go +++ b/pointers/v2/wallet.go @@ -2,24 +2,29 @@ package main import "fmt" +// Bitcoin represents a number of Bitcoins type Bitcoin int func (b Bitcoin) String() string { return fmt.Sprintf("%d BTC", b) } +// Wallet stores the number of Bitcoin someone owns type Wallet struct { balance Bitcoin } +// Deposit will add some Bitcoin to a wallet func (w *Wallet) Deposit(amount Bitcoin) { w.balance += amount } +// Withdraw subtracts some Bitcoin from the wallet func (w *Wallet) Withdraw(amount Bitcoin) { w.balance -= amount } +// Balance returns the number of Bitcoin a wallet has func (w *Wallet) Balance() Bitcoin { return w.balance } diff --git a/pointers/v3/wallet.go b/pointers/v3/wallet.go index a2e8d9944..2e32b16ae 100644 --- a/pointers/v3/wallet.go +++ b/pointers/v3/wallet.go @@ -5,20 +5,24 @@ import ( "fmt" ) +// Bitcoin represents a number of Bitcoins type Bitcoin int func (b Bitcoin) String() string { return fmt.Sprintf("%d BTC", b) } +// Wallet stores the number of Bitcoin someone owns type Wallet struct { balance Bitcoin } +// Deposit will add some Bitcoin to a wallet func (w *Wallet) Deposit(amount Bitcoin) { w.balance += amount } +// Withdraw subtracts some Bitcoin from the wallet, returning an error if it cannot be performed func (w *Wallet) Withdraw(amount Bitcoin) error { if amount > w.balance { @@ -29,6 +33,7 @@ func (w *Wallet) Withdraw(amount Bitcoin) error { return nil } +// Balance returns the number of Bitcoin a wallet has func (w *Wallet) Balance() Bitcoin { return w.balance } diff --git a/pointers/v4/wallet.go b/pointers/v4/wallet.go index 3b1181bac..082b39456 100644 --- a/pointers/v4/wallet.go +++ b/pointers/v4/wallet.go @@ -5,32 +5,38 @@ import ( "fmt" ) +// Bitcoin represents a number of Bitcoins type Bitcoin int func (b Bitcoin) String() string { return fmt.Sprintf("%d BTC", b) } +// Wallet stores the number of Bitcoin someone owns type Wallet struct { balance Bitcoin } +// Deposit will add some Bitcoin to a wallet func (w *Wallet) Deposit(amount Bitcoin) { w.balance += amount } -var InsufficientFundsError = errors.New("cannot withdraw, insufficient funds") +// ErrInsufficientFunds means a wallet does not have enough Bitcoin to perform a withdraw +var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds") +// Withdraw subtracts some Bitcoin from the wallet, returning an error if it cannot be performed func (w *Wallet) Withdraw(amount Bitcoin) error { if amount > w.balance { - return InsufficientFundsError + return ErrInsufficientFunds } w.balance -= amount return nil } +// Balance returns the number of Bitcoin a wallet has func (w *Wallet) Balance() Bitcoin { return w.balance } diff --git a/pointers/v4/wallet_test.go b/pointers/v4/wallet_test.go index e00d47340..56c3c098b 100644 --- a/pointers/v4/wallet_test.go +++ b/pointers/v4/wallet_test.go @@ -27,7 +27,7 @@ func TestWallet(t *testing.T) { err := wallet.Withdraw(Bitcoin(100)) assertBalance(t, wallet, startingBalance) - assertError(t, err, InsufficientFundsError) + assertError(t, err, ErrInsufficientFunds) }) } diff --git a/structs/v1/shapes.go b/structs/v1/shapes.go index cbfc38095..18c275d06 100644 --- a/structs/v1/shapes.go +++ b/structs/v1/shapes.go @@ -1,5 +1,6 @@ package main +// Perimeter returns the perimeter of a rectangle func Perimeter(width float64, height float64) float64 { return 2 * (width + height) } diff --git a/structs/v2/shapes.go b/structs/v2/shapes.go index 5d3376090..506aa6d2e 100644 --- a/structs/v2/shapes.go +++ b/structs/v2/shapes.go @@ -1,9 +1,11 @@ package main +// Perimeter returns the perimeter of a rectangle func Perimeter(width float64, height float64) float64 { return 2 * (width + height) } +// Area returns the area of a rectangle func Area(width float64, height float64) float64 { return width * height } diff --git a/structs/v3/shapes.go b/structs/v3/shapes.go index fc709698f..859d767f6 100644 --- a/structs/v3/shapes.go +++ b/structs/v3/shapes.go @@ -1,14 +1,17 @@ package main +// Rectangle has the dimensions of a rectangle type Rectangle struct { Width float64 Height float64 } +// Perimeter returns the perimeter of the rectangle func Perimeter(rectangle Rectangle) float64 { return 2 * (rectangle.Width + rectangle.Height) } +// Area returns the area of the rectangle func Area(rectangle Rectangle) float64 { return rectangle.Width * rectangle.Height } diff --git a/structs/v4/shapes.go b/structs/v4/shapes.go index 708c71af4..b4003df30 100644 --- a/structs/v4/shapes.go +++ b/structs/v4/shapes.go @@ -2,23 +2,28 @@ package main import "math" +// Rectangle has the dimensions of a rectangle type Rectangle struct { Width float64 Height float64 } +// Area returns the area of the rectangle func (r Rectangle) Area() float64 { return r.Width * r.Height } +// Perimeter returns the perimeter of a rectangle +func Perimeter(rectangle Rectangle) float64 { + return 2 * (rectangle.Width + rectangle.Height) +} + +// Circle represents a circle... type Circle struct { Radius float64 } +// Area returns the area of the circle func (c Circle) Area() float64 { return math.Pi * c.Radius * c.Radius } - -func Perimeter(rectangle Rectangle) float64 { - return 2 * (rectangle.Width + rectangle.Height) -} diff --git a/structs/v5/shapes.go b/structs/v5/shapes.go index 0c880c239..ccde5218d 100644 --- a/structs/v5/shapes.go +++ b/structs/v5/shapes.go @@ -2,27 +2,33 @@ package main import "math" +// Shape is implemented by anything that can tell us its Area type Shape interface { Area() float64 } +// Rectangle has the dimensions of a rectangle type Rectangle struct { Width float64 Height float64 } +// Area returns the area of the rectangle func (r Rectangle) Area() float64 { return r.Width * r.Height } +// Perimeter returns the perimeter of a rectangle +func Perimeter(rectangle Rectangle) float64 { + return 2 * (rectangle.Width + rectangle.Height) +} + +// Circle represents a circle... type Circle struct { Radius float64 } +// Area returns the area of the circle func (c Circle) Area() float64 { return math.Pi * c.Radius * c.Radius } - -func Perimeter(rectangle Rectangle) float64 { - return 2 * (rectangle.Width + rectangle.Height) -} diff --git a/structs/v6/shapes.go b/structs/v6/shapes.go index 0c880c239..ccde5218d 100644 --- a/structs/v6/shapes.go +++ b/structs/v6/shapes.go @@ -2,27 +2,33 @@ package main import "math" +// Shape is implemented by anything that can tell us its Area type Shape interface { Area() float64 } +// Rectangle has the dimensions of a rectangle type Rectangle struct { Width float64 Height float64 } +// Area returns the area of the rectangle func (r Rectangle) Area() float64 { return r.Width * r.Height } +// Perimeter returns the perimeter of a rectangle +func Perimeter(rectangle Rectangle) float64 { + return 2 * (rectangle.Width + rectangle.Height) +} + +// Circle represents a circle... type Circle struct { Radius float64 } +// Area returns the area of the circle func (c Circle) Area() float64 { return math.Pi * c.Radius * c.Radius } - -func Perimeter(rectangle Rectangle) float64 { - return 2 * (rectangle.Width + rectangle.Height) -} diff --git a/structs/v7/shapes.go b/structs/v7/shapes.go index 14036dbdf..e4a46be63 100644 --- a/structs/v7/shapes.go +++ b/structs/v7/shapes.go @@ -2,36 +2,44 @@ package main import "math" +// Shape is implemented by anything that can tell us its Area type Shape interface { Area() float64 } +// Rectangle has the dimensions of a rectangle type Rectangle struct { Width float64 Height float64 } +// Area returns the area of the rectangle func (r Rectangle) Area() float64 { return r.Width * r.Height } +// Perimeter returns the perimeter of a rectangle +func Perimeter(rectangle Rectangle) float64 { + return 2 * (rectangle.Width + rectangle.Height) +} + +// Circle represents a circle... type Circle struct { Radius float64 } +// Area returns the area of the circle func (c Circle) Area() float64 { return math.Pi * c.Radius * c.Radius } +// Triangle represents the dimensions of a triangle type Triangle struct { Base float64 Height float64 } +// Area returns the area of the triangle func (c Triangle) Area() float64 { return (c.Base * c.Height) * 0.5 } - -func Perimeter(rectangle Rectangle) float64 { - return 2 * (rectangle.Width + rectangle.Height) -} diff --git a/structs/v8/shapes.go b/structs/v8/shapes.go index 14036dbdf..e4a46be63 100644 --- a/structs/v8/shapes.go +++ b/structs/v8/shapes.go @@ -2,36 +2,44 @@ package main import "math" +// Shape is implemented by anything that can tell us its Area type Shape interface { Area() float64 } +// Rectangle has the dimensions of a rectangle type Rectangle struct { Width float64 Height float64 } +// Area returns the area of the rectangle func (r Rectangle) Area() float64 { return r.Width * r.Height } +// Perimeter returns the perimeter of a rectangle +func Perimeter(rectangle Rectangle) float64 { + return 2 * (rectangle.Width + rectangle.Height) +} + +// Circle represents a circle... type Circle struct { Radius float64 } +// Area returns the area of the circle func (c Circle) Area() float64 { return math.Pi * c.Radius * c.Radius } +// Triangle represents the dimensions of a triangle type Triangle struct { Base float64 Height float64 } +// Area returns the area of the triangle func (c Triangle) Area() float64 { return (c.Base * c.Height) * 0.5 } - -func Perimeter(rectangle Rectangle) float64 { - return 2 * (rectangle.Width + rectangle.Height) -}