Skip to content

Commit d2e1cf2

Browse files
authored
go fmt code block to reduce formating errors (quii#350)
I used gofmtmd package: ```sh ls -1 *.md | xargs -I % sh -c 'echo %; gofmtmd % -r' ``` There is some errors, in file that could not be parsed
1 parent a6fcd41 commit d2e1cf2

12 files changed

+1179
-1179
lines changed

arrays-and-slices.md

+135-135
Large diffs are not rendered by default.

command-line.md

+141-141
Large diffs are not rendered by default.

concurrency.md

+78-78
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ package concurrency
1111
type WebsiteChecker func(string) bool
1212

1313
func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
14-
results := make(map[string]bool)
14+
results := make(map[string]bool)
1515

16-
for _, url := range urls {
17-
results[url] = wc(url)
18-
}
16+
for _, url := range urls {
17+
results[url] = wc(url)
18+
}
1919

20-
return results
20+
return results
2121
}
2222
```
2323

@@ -36,35 +36,35 @@ Here's the test they've written:
3636
package concurrency
3737

3838
import (
39-
"reflect"
40-
"testing"
39+
"reflect"
40+
"testing"
4141
)
4242

4343
func mockWebsiteChecker(url string) bool {
44-
if url == "waat://furhurterwe.geds" {
45-
return false
46-
}
47-
return true
44+
if url == "waat://furhurterwe.geds" {
45+
return false
46+
}
47+
return true
4848
}
4949

5050
func TestCheckWebsites(t *testing.T) {
51-
websites := []string{
52-
"http://google.com",
53-
"http://blog.gypsydave5.com",
54-
"waat://furhurterwe.geds",
55-
}
56-
57-
want := map[string]bool{
58-
"http://google.com": true,
59-
"http://blog.gypsydave5.com": true,
60-
"waat://furhurterwe.geds": false,
61-
}
62-
63-
got := CheckWebsites(mockWebsiteChecker, websites)
64-
65-
if !reflect.DeepEqual(want, got) {
66-
t.Fatalf("Wanted %v, got %v", want, got)
67-
}
51+
websites := []string{
52+
"http://google.com",
53+
"http://blog.gypsydave5.com",
54+
"waat://furhurterwe.geds",
55+
}
56+
57+
want := map[string]bool{
58+
"http://google.com": true,
59+
"http://blog.gypsydave5.com": true,
60+
"waat://furhurterwe.geds": false,
61+
}
62+
63+
got := CheckWebsites(mockWebsiteChecker, websites)
64+
65+
if !reflect.DeepEqual(want, got) {
66+
t.Fatalf("Wanted %v, got %v", want, got)
67+
}
6868
}
6969
```
7070

@@ -81,24 +81,24 @@ effect of our changes.
8181
package concurrency
8282

8383
import (
84-
"testing"
85-
"time"
84+
"testing"
85+
"time"
8686
)
8787

8888
func slowStubWebsiteChecker(_ string) bool {
89-
time.Sleep(20 * time.Millisecond)
90-
return true
89+
time.Sleep(20 * time.Millisecond)
90+
return true
9191
}
9292

9393
func BenchmarkCheckWebsites(b *testing.B) {
94-
urls := make([]string, 100)
95-
for i := 0; i < len(urls); i++ {
96-
urls[i] = "a url"
97-
}
98-
99-
for i := 0; i < b.N; i++ {
100-
CheckWebsites(slowStubWebsiteChecker, urls)
101-
}
94+
urls := make([]string, 100)
95+
for i := 0; i < len(urls); i++ {
96+
urls[i] = "a url"
97+
}
98+
99+
for i := 0; i < b.N; i++ {
100+
CheckWebsites(slowStubWebsiteChecker, urls)
101+
}
102102
}
103103
```
104104

@@ -158,15 +158,15 @@ package concurrency
158158
type WebsiteChecker func(string) bool
159159

160160
func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
161-
results := make(map[string]bool)
161+
results := make(map[string]bool)
162162

163-
for _, url := range urls {
164-
go func() {
165-
results[url] = wc(url)
166-
}()
167-
}
163+
for _, url := range urls {
164+
go func() {
165+
results[url] = wc(url)
166+
}()
167+
}
168168

169-
return results
169+
return results
170170
}
171171
```
172172

@@ -228,17 +228,17 @@ import "time"
228228
type WebsiteChecker func(string) bool
229229

230230
func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
231-
results := make(map[string]bool)
231+
results := make(map[string]bool)
232232

233-
for _, url := range urls {
234-
go func() {
235-
results[url] = wc(url)
236-
}()
237-
}
233+
for _, url := range urls {
234+
go func() {
235+
results[url] = wc(url)
236+
}()
237+
}
238238

239-
time.Sleep(2 * time.Second)
239+
time.Sleep(2 * time.Second)
240240

241-
return results
241+
return results
242242
}
243243
```
244244

@@ -266,23 +266,23 @@ To fix this:
266266
package concurrency
267267

268268
import (
269-
"time"
269+
"time"
270270
)
271271

272272
type WebsiteChecker func(string) bool
273273

274274
func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
275-
results := make(map[string]bool)
275+
results := make(map[string]bool)
276276

277-
for _, url := range urls {
278-
go func(u string) {
279-
results[u] = wc(u)
280-
}(url)
281-
}
277+
for _, url := range urls {
278+
go func(u string) {
279+
results[u] = wc(u)
280+
}(url)
281+
}
282282

283-
time.Sleep(2 * time.Second)
283+
time.Sleep(2 * time.Second)
284284

285-
return results
285+
return results
286286
}
287287
```
288288

@@ -405,26 +405,26 @@ package concurrency
405405

406406
type WebsiteChecker func(string) bool
407407
type result struct {
408-
string
409-
bool
408+
string
409+
bool
410410
}
411411

412412
func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
413-
results := make(map[string]bool)
414-
resultChannel := make(chan result)
413+
results := make(map[string]bool)
414+
resultChannel := make(chan result)
415415

416-
for _, url := range urls {
417-
go func(u string) {
418-
resultChannel <- result{u, wc(u)}
419-
}(url)
420-
}
416+
for _, url := range urls {
417+
go func(u string) {
418+
resultChannel <- result{u, wc(u)}
419+
}(url)
420+
}
421421

422-
for i := 0; i < len(urls); i++ {
423-
result := <-resultChannel
424-
results[result.string] = result.bool
425-
}
422+
for i := 0; i < len(urls); i++ {
423+
result := <-resultChannel
424+
results[result.string] = result.bool
425+
}
426426

427-
return results
427+
return results
428428
}
429429
```
430430

dependency-injection.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Just to recap, here is what that function could look like
1717

1818
```go
1919
func Greet(name string) {
20-
fmt.Printf("Hello, %s", name)
20+
fmt.Printf("Hello, %s", name)
2121
}
2222
```
2323

@@ -34,7 +34,7 @@ If you look at the source code of `fmt.Printf` you can see a way for us to hook
3434
```go
3535
// It returns the number of bytes written and any write error encountered.
3636
func Printf(format string, a ...interface{}) (n int, err error) {
37-
return Fprintf(os.Stdout, format, a...)
37+
return Fprintf(os.Stdout, format, a...)
3838
}
3939
```
4040

@@ -44,19 +44,19 @@ What exactly _is_ an `os.Stdout`? What does `Fprintf` expect to get passed to it
4444

4545
```go
4646
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
47-
p := newPrinter()
48-
p.doPrintf(format, a)
49-
n, err = w.Write(p.buf)
50-
p.free()
51-
return
47+
p := newPrinter()
48+
p.doPrintf(format, a)
49+
n, err = w.Write(p.buf)
50+
p.free()
51+
return
5252
}
5353
```
5454

5555
An `io.Writer`
5656

5757
```go
5858
type Writer interface {
59-
Write(p []byte) (n int, err error)
59+
Write(p []byte) (n int, err error)
6060
}
6161
```
6262

@@ -68,15 +68,15 @@ So we know under the covers we're ultimately using `Writer` to send our greeting
6868

6969
```go
7070
func TestGreet(t *testing.T) {
71-
buffer := bytes.Buffer{}
72-
Greet(&buffer,"Chris")
71+
buffer := bytes.Buffer{}
72+
Greet(&buffer, "Chris")
7373

74-
got := buffer.String()
75-
want := "Hello, Chris"
74+
got := buffer.String()
75+
want := "Hello, Chris"
7676

77-
if got != want {
78-
t.Errorf("got %q want %q", got, want)
79-
}
77+
if got != want {
78+
t.Errorf("got %q want %q", got, want)
79+
}
8080
}
8181
```
8282

@@ -100,7 +100,7 @@ _Listen to the compiler_ and fix the problem.
100100

101101
```go
102102
func Greet(writer *bytes.Buffer, name string) {
103-
fmt.Printf("Hello, %s", name)
103+
fmt.Printf("Hello, %s", name)
104104
}
105105
```
106106

@@ -114,7 +114,7 @@ Use the writer to send the greeting to the buffer in our test. Remember `fmt.Fpr
114114

115115
```go
116116
func Greet(writer *bytes.Buffer, name string) {
117-
fmt.Fprintf(writer, "Hello, %s", name)
117+
fmt.Fprintf(writer, "Hello, %s", name)
118118
}
119119
```
120120

@@ -128,7 +128,7 @@ To demonstrate this, try wiring up the `Greet` function into a Go application wh
128128

129129
```go
130130
func main() {
131-
Greet(os.Stdout, "Elodie")
131+
Greet(os.Stdout, "Elodie")
132132
}
133133
```
134134

@@ -152,7 +152,7 @@ func Greet(writer io.Writer, name string) {
152152
}
153153

154154
func main() {
155-
Greet(os.Stdout, "Elodie")
155+
Greet(os.Stdout, "Elodie")
156156
}
157157
```
158158

@@ -168,21 +168,21 @@ Run the following
168168
package main
169169

170170
import (
171-
"fmt"
172-
"io"
173-
"net/http"
171+
"fmt"
172+
"io"
173+
"net/http"
174174
)
175175

176176
func Greet(writer io.Writer, name string) {
177-
fmt.Fprintf(writer, "Hello, %s", name)
177+
fmt.Fprintf(writer, "Hello, %s", name)
178178
}
179179

180180
func MyGreeterHandler(w http.ResponseWriter, r *http.Request) {
181-
Greet(w, "world")
181+
Greet(w, "world")
182182
}
183183

184184
func main() {
185-
http.ListenAndServe(":5000", http.HandlerFunc(MyGreeterHandler))
185+
http.ListenAndServe(":5000", http.HandlerFunc(MyGreeterHandler))
186186
}
187187
```
188188

0 commit comments

Comments
 (0)