Skip to content

Commit 92879fe

Browse files
committed
change from master to main
1 parent 346c0ce commit 92879fe

27 files changed

+195
-195
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
[Art by Denise](https://twitter.com/deniseyu21)
88

9-
[![Build Status](https://travis-ci.org/quii/learn-go-with-tests.svg?branch=master)](https://travis-ci.org/quii/learn-go-with-tests)
9+
[![Build Status](https://travis-ci.org/quii/learn-go-with-tests.svg?branch=main)](https://travis-ci.org/quii/learn-go-with-tests)
1010
[![Go Report Card](https://goreportcard.com/badge/github.com/quii/learn-go-with-tests)](https://goreportcard.com/report/github.com/quii/learn-go-with-tests)
1111

1212
## Formats
@@ -16,7 +16,7 @@
1616

1717
## Translations
1818

19-
- [中文](https://studygolang.gitbook.io/learn-go-with-tests)
19+
- [中文](https://studygolang.gitbook.io/learn-go-with-tests)
2020
- [Português](https://larien.gitbook.io/aprenda-go-com-testes/)
2121
- [日本語](https://andmorefine.gitbook.io/learn-go-with-tests/)
2222

arrays-and-slices.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Arrays and slices
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/arrays)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/arrays)**
44

55
Arrays allow you to store multiple elements of the same type in a variable in
66
a particular order.
@@ -559,10 +559,10 @@ Another handy way to experiment with Go other than writing tests is the Go
559559
playground. You can try most things out and you can easily share your code if
560560
you need to ask questions. [I have made a go playground with a slice in it for you to experiment with.](https://play.golang.org/p/ICCWcRGIO68)
561561

562-
[Here is an example](https://play.golang.org/p/bTrRmYfNYCp) of slicing an array
563-
and how changing the slice affects the original array; but a "copy" of the slice
562+
[Here is an example](https://play.golang.org/p/bTrRmYfNYCp) of slicing an array
563+
and how changing the slice affects the original array; but a "copy" of the slice
564564
will not affect the original array.
565-
[Another example](https://play.golang.org/p/Poth8JS28sc) of why it's a good idea
565+
[Another example](https://play.golang.org/p/Poth8JS28sc) of why it's a good idea
566566
to make a copy of a slice after slicing a very large slice.
567567

568568
[for]: ../iteration.md#

command-line.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Command line and project structure
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/command-line)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/command-line)**
44

55
Our product owner now wants to _pivot_ by introducing a second application - a command line application.
66

concurrency.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Concurrency
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/concurrency)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/concurrency)**
44

55
Here's the setup: a colleague has written a function, `CheckWebsites`, that
66
checks the status of a list of URLs.

context-aware-reader.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Context-aware readers
22

3-
**[You can find all the code here](https://github.com/quii/learn-go-with-tests/tree/master/q-and-a/context-aware-reader)**
3+
**[You can find all the code here](https://github.com/quii/learn-go-with-tests/tree/main/q-and-a/context-aware-reader)**
44

55
This chapter demonstrates how to test-drive a context aware `io.Reader` as written by Mat Ryer and David Hernandez in [The Pace Dev Blog](https://pace.dev/blog/2020/02/03/context-aware-ioreader-for-golang-by-mat-ryer).
66

context.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Context
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/context)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/context)**
44

5-
Software often kicks off long-running, resource-intensive processes (often in goroutines). If the action that caused this gets cancelled or fails for some reason you need to stop these processes in a consistent way through your application.
5+
Software often kicks off long-running, resource-intensive processes (often in goroutines). If the action that caused this gets cancelled or fails for some reason you need to stop these processes in a consistent way through your application.
66

7-
If you don't manage this your snappy Go application that you're so proud of could start having difficult to debug performance problems.
7+
If you don't manage this your snappy Go application that you're so proud of could start having difficult to debug performance problems.
88

99
In this chapter we'll use the package `context` to help us manage long-running processes.
1010

11-
We're going to start with a classic example of a web server that when hit kicks off a potentially long-running process to fetch some data for it to return in the response.
11+
We're going to start with a classic example of a web server that when hit kicks off a potentially long-running process to fetch some data for it to return in the response.
1212

13-
We will exercise a scenario where a user cancels the request before the data can be retrieved and we'll make sure the process is told to give up.
13+
We will exercise a scenario where a user cancels the request before the data can be retrieved and we'll make sure the process is told to give up.
1414

1515
I've set up some code on the happy path to get us started. Here is our server code.
1616

@@ -95,17 +95,17 @@ Let's add a new test where we cancel the request before 100 milliseconds and che
9595
t.Run("tells store to cancel work if request is cancelled", func(t *testing.T) {
9696
store := &SpyStore{response: data}
9797
svr := Server(store)
98-
98+
9999
request := httptest.NewRequest(http.MethodGet, "/", nil)
100-
100+
101101
cancellingCtx, cancel := context.WithCancel(request.Context())
102102
time.AfterFunc(5 * time.Millisecond, cancel)
103103
request = request.WithContext(cancellingCtx)
104-
104+
105105
response := httptest.NewRecorder()
106-
106+
107107
svr.ServeHTTP(response, request)
108-
108+
109109
if !store.cancelled {
110110
t.Errorf("store was not told to cancel")
111111
}
@@ -116,7 +116,7 @@ From the [Go Blog: Context](https://blog.golang.org/context)
116116

117117
> The context package provides functions to derive new Context values from existing ones. These values form a tree: when a Context is canceled, all Contexts derived from it are also canceled.
118118
119-
It's important that you derive your contexts so that cancellations are propagated throughout the call stack for a given request.
119+
It's important that you derive your contexts so that cancellations are propagated throughout the call stack for a given request.
120120

121121
What we do is derive a new `cancellingCtx` from our `request` which returns us a `cancel` function. We then schedule that function to be called in 5 milliseconds by using `time.AfterFunc`. Finally we use this new context in our request by calling `request.WithContext`.
122122

@@ -132,7 +132,7 @@ The test fails as we'd expect.
132132

133133
## Write enough code to make it pass
134134

135-
Remember to be disciplined with TDD. Write the _minimal_ amount of code to make our test pass.
135+
Remember to be disciplined with TDD. Write the _minimal_ amount of code to make our test pass.
136136

137137
```go
138138
func Server(store Store) http.HandlerFunc {
@@ -143,11 +143,11 @@ func Server(store Store) http.HandlerFunc {
143143
}
144144
```
145145

146-
This makes this test pass but it doesn't feel good does it! We surely shouldn't be cancelling `Store` before we fetch on _every request_.
146+
This makes this test pass but it doesn't feel good does it! We surely shouldn't be cancelling `Store` before we fetch on _every request_.
147147

148-
By being disciplined it highlighted a flaw in our tests, this is a good thing!
148+
By being disciplined it highlighted a flaw in our tests, this is a good thing!
149149

150-
We'll need to update our happy path test to assert that it does not get cancelled.
150+
We'll need to update our happy path test to assert that it does not get cancelled.
151151

152152
```go
153153
t.Run("returns data from store", func(t *testing.T) {
@@ -162,7 +162,7 @@ t.Run("returns data from store", func(t *testing.T) {
162162
if response.Body.String() != data {
163163
t.Errorf(`got "%s", want "%s"`, response.Body.String(), data)
164164
}
165-
165+
166166
if store.cancelled {
167167
t.Error("it should not have cancelled the store")
168168
}
@@ -218,7 +218,7 @@ func (s *SpyStore) assertWasNotCancelled() {
218218
}
219219
```
220220

221-
Remember to pass in the `*testing.T` when creating the spy.
221+
Remember to pass in the `*testing.T` when creating the spy.
222222

223223
```go
224224
func TestServer(t *testing.T) {
@@ -259,11 +259,11 @@ func TestServer(t *testing.T) {
259259
}
260260
```
261261

262-
This approach is ok, but is it idiomatic?
262+
This approach is ok, but is it idiomatic?
263263

264-
Does it make sense for our web server to be concerned with manually cancelling `Store`? What if `Store` also happens to depend on other slow-running processes? We'll have to make sure that `Store.Cancel` correctly propagates the cancellation to all of its dependants.
264+
Does it make sense for our web server to be concerned with manually cancelling `Store`? What if `Store` also happens to depend on other slow-running processes? We'll have to make sure that `Store.Cancel` correctly propagates the cancellation to all of its dependants.
265265

266-
One of the main points of `context` is that it is a consistent way of offering cancellation.
266+
One of the main points of `context` is that it is a consistent way of offering cancellation.
267267

268268
[From the go doc](https://golang.org/pkg/context/)
269269

@@ -281,7 +281,7 @@ Feeling a bit uneasy? Good. Let's try and follow that approach though and instea
281281

282282
We'll have to change our existing tests as their responsibilities are changing. The only thing our handler is responsible for now is making sure it sends a context through to the downstream `Store` and that it handles the error that will come from the `Store` when it is cancelled.
283283

284-
Let's update our `Store` interface to show the new responsibilities.
284+
Let's update our `Store` interface to show the new responsibilities.
285285

286286
```go
287287
type Store interface {
@@ -333,13 +333,13 @@ func (s *SpyStore) Fetch(ctx context.Context) (string, error) {
333333
}
334334
```
335335

336-
We have to make our spy act like a real method that works with `context`.
336+
We have to make our spy act like a real method that works with `context`.
337337

338-
We are simulating a slow process where we build the result slowly by appending the string, character by character in a goroutine. When the goroutine finishes its work it writes the string to the `data` channel. The goroutine listens for the `ctx.Done` and will stop the work if a signal is sent in that channel.
338+
We are simulating a slow process where we build the result slowly by appending the string, character by character in a goroutine. When the goroutine finishes its work it writes the string to the `data` channel. The goroutine listens for the `ctx.Done` and will stop the work if a signal is sent in that channel.
339339

340340
Finally the code uses another `select` to wait for that goroutine to finish its work or for the cancellation to occur.
341341

342-
It's similar to our approach from before, we use Go's concurrency primitives to make two asynchronous processes race each other to determine what we return.
342+
It's similar to our approach from before, we use Go's concurrency primitives to make two asynchronous processes race each other to determine what we return.
343343

344344
You'll take a similar approach when writing your own functions and methods that accept a `context` so make sure you understand what's going on.
345345

@@ -385,7 +385,7 @@ Our happy path should be... happy. Now we can fix the other test.
385385

386386
## Write the test first
387387

388-
We need to test that we do not write any kind of response on the error case. Sadly `httptest.ResponseRecorder` doesn't have a way of figuring this out so we'll have to role our own spy to test for this.
388+
We need to test that we do not write any kind of response on the error case. Sadly `httptest.ResponseRecorder` doesn't have a way of figuring this out so we'll have to role our own spy to test for this.
389389

390390
```go
391391
type SpyResponseWriter struct {
@@ -450,13 +450,13 @@ func Server(store Store) http.HandlerFunc {
450450
if err != nil {
451451
return // todo: log error however you like
452452
}
453-
453+
454454
fmt.Fprint(w, data)
455455
}
456456
}
457457
```
458458

459-
We can see after this that the server code has become simplified as it's no longer explicitly responsible for cancellation, it simply passes through `context` and relies on the downstream functions to respect any cancellations that may occur.
459+
We can see after this that the server code has become simplified as it's no longer explicitly responsible for cancellation, it simply passes through `context` and relies on the downstream functions to respect any cancellations that may occur.
460460

461461
## Wrapping up
462462

@@ -474,13 +474,13 @@ We can see after this that the server code has become simplified as it's no long
474474

475475
> If you use ctx.Value in my (non-existent) company, you’re fired
476476
477-
Some engineers have advocated passing values through `context` as it _feels convenient_.
477+
Some engineers have advocated passing values through `context` as it _feels convenient_.
478478

479-
Convenience is often the cause of bad code.
479+
Convenience is often the cause of bad code.
480480

481-
The problem with `context.Values` is that it's just an untyped map so you have no type-safety and you have to handle it not actually containing your value. You have to create a coupling of map keys from one module to another and if someone changes something things start breaking.
481+
The problem with `context.Values` is that it's just an untyped map so you have no type-safety and you have to handle it not actually containing your value. You have to create a coupling of map keys from one module to another and if someone changes something things start breaking.
482482

483-
In short, **if a function needs some values, put them as typed parameters rather than trying to fetch them from `context.Value`**. This makes is statically checked and documented for everyone to see.
483+
In short, **if a function needs some values, put them as typed parameters rather than trying to fetch them from `context.Value`**. This makes is statically checked and documented for everyone to see.
484484

485485
#### But...
486486

dependency-injection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Dependency Injection
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/di)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/di)**
44

55
It is assumed that you have read the structs section before as some understanding of interfaces will be needed for this.
66

error-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Error types
22

3-
**[You can find all the code here](https://github.com/quii/learn-go-with-tests/tree/master/q-and-a/error-types)**
3+
**[You can find all the code here](https://github.com/quii/learn-go-with-tests/tree/main/q-and-a/error-types)**
44

55
**Creating your own types for errors can be an elegant way of tidying up your code, making your code easier to use and test.**
66

hello-world.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Hello, World
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/hello-world)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/hello-world)**
44

5-
It is traditional for your first program in a new language to be [Hello, World](https://en.m.wikipedia.org/wiki/%22Hello,_World!%22_program).
5+
It is traditional for your first program in a new language to be [Hello, World](https://en.m.wikipedia.org/wiki/%22Hello,_World!%22_program).
66

77
In the [previous chapter](install-go.md#go-environment) we discussed how Go is opinionated as to where you put your files.
88

http-handlers-revisited.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# HTTP Handlers Revisited
22

3-
**[You can find all the code here](https://github.com/quii/learn-go-with-tests/tree/master/q-and-a/http-handlers-revisited)**
3+
**[You can find all the code here](https://github.com/quii/learn-go-with-tests/tree/main/q-and-a/http-handlers-revisited)**
44

55
This book already has a chapter on [testing a HTTP handler](http-server.md) but this will feature a broader discussion on designing them, so they are simple to test.
66

http-server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# HTTP Server
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/http-server)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/http-server)**
44

55
You have been asked to create a web server where users can track how many games players have won.
66

integers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Integers
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/integers)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/integers)**
44

55
Integers work as you would expect. Let's write an `Add` function to try things out. Create a test file called `adder_test.go` and write this code.
66

io.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# IO and sorting
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/io)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/io)**
44

55
[In the previous chapter](json.md) we continued iterating on our application by adding a new endpoint `/league`. Along the way we learned about how to deal with JSON, embedding types and routing.
66

@@ -829,7 +829,7 @@ func NewFileSystemPlayerStore(database io.ReadWriteSeeker) *FileSystemPlayerStor
829829

830830
Finally, we can get the amazing payoff we wanted by removing the `Seek` call from `RecordWin`. Yes, it doesn't feel much, but at least it means if we do any other kind of writes we can rely on our `Write` to behave how we need it to. Plus it will now let us test the potentially problematic code separately and fix it.
831831

832-
Let's write the test where we want to update the entire contents of a file with something that is smaller than the original contents.
832+
Let's write the test where we want to update the entire contents of a file with something that is smaller than the original contents.
833833

834834
## Write the test first
835835

iteration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Iteration
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/for)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/for)**
44

55
To do stuff repeatedly in Go, you'll need `for`. In Go there are no `while`, `do`, `until` keywords, you can only use `for`. Which is a good thing!
66

json.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# JSON, routing & embedding
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/json)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/json)**
44

55
[In the previous chapter](http-server.md) we created a web server to store how many games players have won.
66

maps.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Maps
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/maps)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/maps)**
44

55
In [arrays & slices](arrays-and-slices.md), you saw how to store values in order. Now, we will look at a way to store items by a `key` and look them up quickly.
66

math.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mathematics
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/math)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/math)**
44

55
For all the power of modern computers to perform huge sums at
66
lightning speed, the average developer rarely uses any mathematics

mocking.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mocking
22

3-
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/master/mocking)**
3+
**[You can find all the code for this chapter here](https://github.com/quii/learn-go-with-tests/tree/main/mocking)**
44

55
You have been asked to write a program which counts down from 3, printing each number on a new line (with a 1 second pause) and when it reaches zero it will print "Go!" and exit.
66

0 commit comments

Comments
 (0)