From 652281d1cf2bbf558513421f1f91afd8e55959aa Mon Sep 17 00:00:00 2001 From: dario-piotrowicz Date: Sat, 11 May 2024 14:18:27 +0100 Subject: [PATCH] fix the incorrect order of explanation of the `.Error()` method --- maps.md | 3 ++- pointers-and-errors.md | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/maps.md b/maps.md index a2ed061b4..0b8273b49 100644 --- a/maps.md +++ b/maps.md @@ -155,7 +155,8 @@ func TestSearch(t *testing.T) { The way to handle this scenario in Go is to return a second argument which is an `Error` type. -`Error`s can be converted to a string with the `.Error()` method, which we do when passing it to the assertion. We are also protecting `assertStrings` with `if` to ensure we don't call `.Error()` on `nil`. +Notice that as we've seen in the [pointers and error section](./pointers-and-errors.md) here in order to asset the error message +we first check that the error is not `nil` and then use `.Error()` method to get the string which we can then pass to the assertion. ## Try and run the test diff --git a/pointers-and-errors.md b/pointers-and-errors.md index d930e1885..975998b0f 100644 --- a/pointers-and-errors.md +++ b/pointers-and-errors.md @@ -455,6 +455,7 @@ Update our helper for a `string` to compare against. ```go assertError := func(t testing.TB, got error, want string) { t.Helper() + if got == nil { t.Fatal("didn't get an error but wanted one") } @@ -465,6 +466,8 @@ assertError := func(t testing.TB, got error, want string) { } ``` +As you can see `Error`s can be converted to a string with the `.Error()` method, which we do in order to compare it with the string we want. We are also making sure that the error is not `nil` to ensure we don't call `.Error()` on `nil`. + And then update the caller ```go