Skip to content

Commit

Permalink
lint issues fixed and check is wired up to build quii#37
Browse files Browse the repository at this point in the history
  • Loading branch information
quii committed Apr 10, 2018
1 parent 428d294 commit dc3c22f
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 34 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ go:

install: true

script: go test ./...
before_script:
- go get github.com/golang/lint/golint

script:
- ./build.sh
2 changes: 1 addition & 1 deletion arrays/v6/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash

set -e

go test ./...
go vet ./...
go fmt ./...
# golint ./...
golint ./...
2 changes: 1 addition & 1 deletion di/v2/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
5 changes: 3 additions & 2 deletions mocking/v3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
10 changes: 5 additions & 5 deletions pointers-and-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
}

Expand All @@ -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

Expand Down Expand Up @@ -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)
})
}

Expand Down
4 changes: 4 additions & 0 deletions pointers/v1/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions pointers/v2/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions pointers/v3/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
10 changes: 8 additions & 2 deletions pointers/v4/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pointers/v4/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
1 change: 1 addition & 0 deletions structs/v1/shapes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package main

// Perimeter returns the perimeter of a rectangle
func Perimeter(width float64, height float64) float64 {
return 2 * (width + height)
}
2 changes: 2 additions & 0 deletions structs/v2/shapes.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions structs/v3/shapes.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 9 additions & 4 deletions structs/v4/shapes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 10 additions & 4 deletions structs/v5/shapes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 10 additions & 4 deletions structs/v6/shapes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading

0 comments on commit dc3c22f

Please sign in to comment.