Permalink
Browse files

post-recording code adjustment

  • Loading branch information...
GoesToEleven committed Sep 26, 2016
1 parent 755b29a commit 67f6eb26cef497f4163adb1b9de102ca33e29be5
View
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/GoesToEleven/GolangTraining/02_package/stringutil"
"github.com/GoesToEleven/GolangTraining/02_package/icomefromalaska"
//someAlias "github.com/GoesToEleven/GolangTraining/02_package/icomefromalaska"
)
func main() {
@@ -8,7 +8,7 @@ import (
)
func main() {
res, err := http.Get("http://www.mcleods.com/")
res, err := http.Get("http://www.geekwiseacademy.com/")
if err != nil {
log.Fatal(err)
}
@@ -7,8 +7,8 @@ import (
)
func main() {
res, _ := http.Get("http://www.mcleods.com/")
res, _ := http.Get("http://www.geekwiseacademy.com/")
page, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
fmt.Printf("%s", page)
}
}
@@ -21,5 +21,6 @@ func info(z shape) {
func main() {
s := square{10}
fmt.Printf("%T\n",s)
info(s)
}
@@ -3,10 +3,11 @@ package main
import (
"fmt"
"time"
"sync/atomic"
)
var workerID int
var publisherID int
var workerID int64
var publisherID int64
func main() {
input := make(chan string)
@@ -22,8 +23,12 @@ func main() {
// publisher pushes data into a channel
func publisher(out chan string) {
publisherID++
thisID := publisherID
atomic.AddInt64(&publisherID, 1)
// atomic was added after recording to fix a race condition
// discover race conditions with the -race flag
// for example: go run -race main.go
// learn about the atomic package: https://godoc.org/sync/atomic#AddInt64
thisID := atomic.LoadInt64(&publisherID)
dataID := 0
for {
dataID++
@@ -34,8 +39,12 @@ func publisher(out chan string) {
}
func workerProcess(in <-chan string) {
workerID++
thisID := workerID
atomic.AddInt64(&workerID, 1)
// atomic was added after recording to fix a race condition
// discover race conditions with the -race flag
// for example: go run -race main.go
// learn about the atomic package: https://godoc.org/sync/atomic#AddInt64
thisID := atomic.LoadInt64(&workerID)
for {
fmt.Printf("%d: waiting for input...\n", thisID)
input := <-in

0 comments on commit 67f6eb2

Please sign in to comment.