/learn-go-with-tests

Going through https://github.com/quii/learn-go-with-tests

Primary LanguageGo

Learn Go With Tests

https://quii.gitbook.io/learn-go-with-tests/

Install Go

Set up environment for productivity.

:shipit: Install Go && setup env

brew install go

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

mkdir -p $GOPATH/src $GOPATH/pkg $GOPATH/bin

go version

:shipit: Go 1.11 introduced Modules as alternative to GOPATH

go mod init <modulepath>

:shipit: In go.mod

module cmd

go 1.14

require (
        github.com/google/pprof v0.0.0-20190515194954-54271f7e092f
        golang.org/x/arch v0.0.0-20190815191158-8a70ba74b3a1
        golang.org/x/tools v0.0.0-20190611154301-25a4f137592f
)

:shipit: Help for go mod

go help mod
go help mod init

:shipit: If using Visual Studio Code

code --install-extension golang.go

:shipit: Install Delve for debugging

go get -u github.com/go-delve/delve/cmd/dlv

:shipit: Go Linter

brew install golangci/tap/golangci-lint

Hello, world

Declaring variables, constants, if/else statements, switch, write your first go program and write your first test. Sub-test syntax and closures.

First App

:shipit: Go apps need to live in $GOPATH?

mkdir -p $GOPATH/src/github.com/$USER/hello

🔥 figured out how to build apps in a local dir instead of $GOPATH with modules

First Test

🚢 23ebc5c Our first test

  • t.Errorf fails test and prints a message

Go Docs

Run Go docs

godoc -http :8000

Args & constants

🚢 58a84c9 method arguments

🚢 86a5030 Using constants const englishHelloPrefix = Hello,

🚢 be312af Go does not have default values for method args

Test Helpers

🚢 79c79e9 refactor tests with a t.Helper()

Switch statement

🚢 803e3f4 Add language support with if statements

🚢 5e3e90f refactor if statements with switch

Examples

Further Explore function declaration syntax and learn new ways to improve the documentation of your code.

:neckbeard: I'm using the suggestions for setting up Go projects here https://dave.cheney.net/2014/12/01/five-suggestions-for-setting-up-a-go-project. Note the package and module declarations.

🚢 184e090 First adder and experiment with Go package and module naming

🚢 0d3913e sample Example that can be verified by running godoc

  • Examples are testable code blocks that can be displayed as package documentation

You can also go test -v to run the tests and examples

🚢 0b5ef61 go-linter wants functions to have comment describing them

Iteration

Learn about for and benchmarking.

for loops

In Go there are no while, do, until keywords, you can only use for

🔥 := is shorthand for declare and initialize. These 2 are the same:

  • i := 5
  • var i int; i = 5

🚢 ddf8062 Iterating with for

+= assignment

🚢 5b85a7c refactor with += assignment

Benchmarking

Go has built in benchmarking with func BenchmarkMyFunc(b *testing.B)

🚢 612097b Benchmark the Repeat func

Run it with go test -bench=.

Practice exercises

🚢 2670235 Change the test so a caller can specify how many times the character is repeated and then fix the code

🚢 b68517e Write ExampleRepeat to document your function

Have a look through the strings package. Find functions you think could be useful and experiment with them by writing tests like we have here. Investing time learning the standard library will really pay off over time.

Arrays and slices

Learn about arrays, slices, len, varargs, range and test coverage.

Array

🚢 21e9528 Iterating over an array

Range

🚢 0fdb6dd Iterating over an array with range

🔦 Integers in Go encode the size in the declaration eg. [4]int for an array holding 4 integers

Slice

For dynamic size arrays use slice instead

🚢 a3b01eb Use a Slice instead of an Array

🚢 6dcb276 Summing multiple arrays with variadic functions

🔥 There's more exercises in this chapter I skipped

Structs, methods & interfaces

Learn about struct, methods, interface and table driven tests.

Want to write a function to calculate Perimeter(width float64, height float64)

🚢 d6ca749 Using float64, Perimeter func and test

🚢 9172b4e Rename perimeter.go to shapes.go

🚢 e029b23 func Area

🚢 ced1f83 Using type Rectangle struct

🚢 0bb9eec https://quii.gitbook.io/learn-go-with-tests/go-fundamentals/pointers-and-errors

Pointers & errors

Learn about pointers and errors.

Maps

Learn about storing values in the map data structure.

Dependency Injection

Learn about dependency injection, how it relates to using interfaces and a primer on io.

Mocking

Take some existing untested code and use DI with mocking to test it.

Concurrency

Learn how to write concurrent code to make your software faster.

Select

Learn how to synchronise asynchronous processes elegantly.

Reflection

Learn about reflection

Sync

Learn some functionality from the sync package including WaitGroup and Mutex

Context

Use the context package to manage and cancel long-running processes

Intro to property based tests

Practice some TDD with the Roman Numerals kata and get a brief intro to property based tests

Maths

Use the math package to draw an SVG clock