https://quii.gitbook.io/learn-go-with-tests/
Set up environment for productivity.
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
Go 1.11 introduced
Modules as alternative to
GOPATH
go mod init <modulepath>
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
)
Help for
go mod
go help mod
go help mod init
If using Visual Studio Code
code --install-extension golang.go
Install Delve for debugging
go get -u github.com/go-delve/delve/cmd/dlv
Go Linter
brew install golangci/tap/golangci-lint
Declaring variables, constants, if/else statements, switch, write your first go program and write your first test. Sub-test syntax and closures.
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
🚢 23ebc5c Our first test
t.Errorf
fails test and prints a message
Run Go docs
godoc -http :8000
- http://localhost:8000/pkg to see installed packages
- http://localhost:8000/pkg/testing/ to see testing package
🚢 58a84c9 method arguments
🚢 86a5030
Using constants const englishHelloPrefix = Hello,
🚢 be312af Go does not have default values for method args
🚢 79c79e9
refactor tests with a t.Helper()
🚢 803e3f4
Add language support with if
statements
🚢 5e3e90f
refactor if
statements with switch
Further Explore function declaration syntax and learn new ways to improve the documentation of your code.
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
Learn about for and benchmarking.
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
🚢 5b85a7c
refactor with +=
assignment
Go has built in benchmarking with func BenchmarkMyFunc(b *testing.B)
🚢 612097b Benchmark the Repeat func
Run it with go test -bench=.
🚢 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.
Learn about arrays, slices, len, varargs, range and test coverage.
🚢 21e9528 Iterating over an array
🚢 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
For dynamic size arrays use slice instead
🚢 a3b01eb Use a Slice instead of an Array
🚢 6dcb276 Summing multiple arrays with variadic functions
- Have to use reflect.DeepEqual
🔥 There's more exercises in this chapter I skipped
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
Learn about pointers and errors.
Learn about storing values in the map data structure.
Learn about dependency injection, how it relates to using interfaces and a primer on io.
Take some existing untested code and use DI with mocking to test it.
Learn how to write concurrent code to make your software faster.
Learn how to synchronise asynchronous processes elegantly.
Learn about reflection
Learn some functionality from the sync package including WaitGroup and Mutex
Use the context package to manage and cancel long-running processes
Practice some TDD with the Roman Numerals kata and get a brief intro to property based tests
Use the math package to draw an SVG clock