Skip to content

Commit 3e326d1

Browse files
committed
Use %q rather than '%s'
1 parent 5bbe246 commit 3e326d1

File tree

8 files changed

+15
-13
lines changed

8 files changed

+15
-13
lines changed

hello-world.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestHello(t *testing.T) {
6464
want := "Hello, world"
6565

6666
if got != want {
67-
t.Errorf("got '%s' want '%s'", got, want)
67+
t.Errorf("got %q want %q", got, want)
6868
}
6969
}
7070
```
@@ -94,7 +94,9 @@ We're declaring some variables with the syntax `varName := value`, which lets us
9494

9595
#### `t.Errorf`
9696

97-
We are calling the `Errorf` _method_ on our `t` which will print out a message and fail the test. The `f` stands for format which allows us to build a string with values inserted into the placeholder values `%s`. When you made the test fail it should be clear how it works.
97+
We are calling the `Errorf` _method_ on our `t` which will print out a message and fail the test. The `f` stands for format which allows us to build a string with values inserted into the placeholder values `%q`. When you made the test fail it should be clear how it works.
98+
99+
You can read more about the placeholder strings in the [fmt go doc](https://golang.org/pkg/fmt/#hdr-Printing). For tests `%q` is very useful as it wraps your values in double quotes.
98100

99101
We will later explore the difference between methods and functions.
100102

@@ -124,7 +126,7 @@ func TestHello(t *testing.T) {
124126
want := "Hello, Chris"
125127

126128
if got != want {
127-
t.Errorf("got '%s' want '%s'", got, want)
129+
t.Errorf("got %q want %q", got, want)
128130
}
129131
}
130132
```
@@ -223,7 +225,7 @@ func TestHello(t *testing.T) {
223225
want := "Hello, Chris"
224226

225227
if got != want {
226-
t.Errorf("got '%s' want '%s'", got, want)
228+
t.Errorf("got %q want %q", got, want)
227229
}
228230
})
229231

@@ -232,7 +234,7 @@ func TestHello(t *testing.T) {
232234
want := "Hello, World"
233235

234236
if got != want {
235-
t.Errorf("got '%s' want '%s'", got, want)
237+
t.Errorf("got %q want %q", got, want)
236238
}
237239
})
238240

@@ -257,7 +259,7 @@ func TestHello(t *testing.T) {
257259
assertCorrectMessage := func(t *testing.T, got, want string) {
258260
t.Helper()
259261
if got != want {
260-
t.Errorf("got '%s' want '%s'", got, want)
262+
t.Errorf("got %q want %q", got, want)
261263
}
262264
}
263265

hello-world/v2/hello_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ func TestHello(t *testing.T) {
77
want := "Hello, world"
88

99
if got != want {
10-
t.Errorf("got '%s' want '%s'", got, want)
10+
t.Errorf("got %q want %q", got, want)
1111
}
1212
}

hello-world/v3/hello_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ func TestHello(t *testing.T) {
77
want := "Hello, Chris"
88

99
if got != want {
10-
t.Errorf("got '%s' want '%s'", got, want)
10+
t.Errorf("got %q want %q", got, want)
1111
}
1212
}

hello-world/v4/hello_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ func TestHello(t *testing.T) {
77
want := "Hello, Chris"
88

99
if got != want {
10-
t.Errorf("got '%s' want '%s'", got, want)
10+
t.Errorf("got %q want %q", got, want)
1111
}
1212
}

hello-world/v5/hello_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func TestHello(t *testing.T) {
77
assertCorrectMessage := func(t *testing.T, got, want string) {
88
t.Helper()
99
if got != want {
10-
t.Errorf("got '%s' want '%s'", got, want)
10+
t.Errorf("got %q want %q", got, want)
1111
}
1212
}
1313

hello-world/v6/hello_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func TestHello(t *testing.T) {
77
assertCorrectMessage := func(t *testing.T, got, want string) {
88
t.Helper()
99
if got != want {
10-
t.Errorf("got '%s' want '%s'", got, want)
10+
t.Errorf("got %q want %q", got, want)
1111
}
1212
}
1313

hello-world/v7/hello_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func TestHello(t *testing.T) {
77
assertCorrectMessage := func(got, want string) {
88
t.Helper()
99
if got != want {
10-
t.Errorf("got '%s' want '%s'", got, want)
10+
t.Errorf("got %q want %q", got, want)
1111
}
1212
}
1313

hello-world/v8/hello_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func TestHello(t *testing.T) {
77
assertCorrectMessage := func(t *testing.T, got, want string) {
88
t.Helper()
99
if got != want {
10-
t.Errorf("got '%s' want '%s'", got, want)
10+
t.Errorf("got %q want %q", got, want)
1111
}
1212
}
1313

0 commit comments

Comments
 (0)