Skip to content

Commit 67f6eb2

Browse files
committed
post-recording code adjustment
1 parent 755b29a commit 67f6eb2

File tree

5 files changed

+20
-9
lines changed
  • 02_package/main
  • 05_blank-identifier/02_http-get_example
  • 21_interfaces/01_interface/02_interface
  • 22_go-routines/13_channels_fan-out_fan-in/05_challenge-solution

5 files changed

+20
-9
lines changed

02_package/main/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/GoesToEleven/GolangTraining/02_package/stringutil"
66
"github.com/GoesToEleven/GolangTraining/02_package/icomefromalaska"
7+
//someAlias "github.com/GoesToEleven/GolangTraining/02_package/icomefromalaska"
78
)
89

910
func main() {

05_blank-identifier/02_http-get_example/01_with-error-checking/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func main() {
11-
res, err := http.Get("http://www.mcleods.com/")
11+
res, err := http.Get("http://www.geekwiseacademy.com/")
1212
if err != nil {
1313
log.Fatal(err)
1414
}

05_blank-identifier/02_http-get_example/02_no-error-checking/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
func main() {
10-
res, _ := http.Get("http://www.mcleods.com/")
10+
res, _ := http.Get("http://www.geekwiseacademy.com/")
1111
page, _ := ioutil.ReadAll(res.Body)
1212
res.Body.Close()
1313
fmt.Printf("%s", page)
14-
}
14+
}

21_interfaces/01_interface/02_interface/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ func info(z shape) {
2121

2222
func main() {
2323
s := square{10}
24+
fmt.Printf("%T\n",s)
2425
info(s)
2526
}

22_go-routines/13_channels_fan-out_fan-in/05_challenge-solution/main.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package main
33
import (
44
"fmt"
55
"time"
6+
"sync/atomic"
67
)
78

8-
var workerID int
9-
var publisherID int
9+
var workerID int64
10+
var publisherID int64
1011

1112
func main() {
1213
input := make(chan string)
@@ -22,8 +23,12 @@ func main() {
2223

2324
// publisher pushes data into a channel
2425
func publisher(out chan string) {
25-
publisherID++
26-
thisID := publisherID
26+
atomic.AddInt64(&publisherID, 1)
27+
// atomic was added after recording to fix a race condition
28+
// discover race conditions with the -race flag
29+
// for example: go run -race main.go
30+
// learn about the atomic package: https://godoc.org/sync/atomic#AddInt64
31+
thisID := atomic.LoadInt64(&publisherID)
2732
dataID := 0
2833
for {
2934
dataID++
@@ -34,8 +39,12 @@ func publisher(out chan string) {
3439
}
3540

3641
func workerProcess(in <-chan string) {
37-
workerID++
38-
thisID := workerID
42+
atomic.AddInt64(&workerID, 1)
43+
// atomic was added after recording to fix a race condition
44+
// discover race conditions with the -race flag
45+
// for example: go run -race main.go
46+
// learn about the atomic package: https://godoc.org/sync/atomic#AddInt64
47+
thisID := atomic.LoadInt64(&workerID)
3948
for {
4049
fmt.Printf("%d: waiting for input...\n", thisID)
4150
input := <-in

0 commit comments

Comments
 (0)