Skip to content

Commit ce45baa

Browse files
committed
init
1 parent 2071deb commit ce45baa

File tree

106 files changed

+2998
-960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+2998
-960
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
# GoProgrammingFromBeginnerToMaster
22
《Go语言精进之路》1、2册书籍配套源代码
3+
<<<<<<< HEAD
4+
5+
本套书的勘误表可在[这里](https://github.com/bigwhite/GoProgrammingFromBeginnerToMaster/blob/main/errata.md)查看。
6+
=======
7+
>>>>>>> 6d6f15e (Initial commit)

chapter1/sources/sieve.go

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package main
22

3+
import "fmt"
4+
35
// Send the sequence 2, 3, 4, ... to channel 'ch'.
46
func Generate(ch chan<- int) {
57
for i := 2; ; i++ {
68
ch <- i // Send 'i' to channel 'ch'.
9+
fmt.Printf("Generate: send to in channel: %d\n", i)
710
}
811
}
912

@@ -12,21 +15,57 @@ func Generate(ch chan<- int) {
1215
func Filter(in <-chan int, out chan<- int, prime int) {
1316
for {
1417
i := <-in // Receive value from 'in'.
18+
fmt.Printf("Filter: receive from in channel: %d\n", i)
1519
if i%prime != 0 {
1620
out <- i // Send 'i' to 'out'.
21+
fmt.Printf("Filter: send to out channel: %d\n", i)
1722
}
1823
}
1924
}
2025

2126
// The prime sieve: Daisy-chain Filter processes.
22-
func main() {
27+
func sieve() {
2328
ch := make(chan int) // Create a new channel.
2429
go Generate(ch) // Launch Generate goroutine.
2530
for i := 0; i < 10; i++ {
2631
prime := <-ch
27-
print(prime, "\n")
32+
fmt.Printf("main: receive from in channel: %d\n", prime)
2833
ch1 := make(chan int)
2934
go Filter(ch, ch1, prime)
3035
ch = ch1
3136
}
3237
}
38+
39+
func main() {
40+
sieve2()
41+
}
42+
43+
func generate2(ch chan int) {
44+
for i := 2; i < 10; i++ {
45+
ch <- i
46+
fmt.Printf("Generate: send to in channel: %d\n", i)
47+
}
48+
}
49+
50+
func filter2(in <-chan int, out chan<- int, prime int) {
51+
for {
52+
i := <-in
53+
fmt.Printf("Filter: receive from in channel: %d\n", i)
54+
if i%prime != 0 {
55+
out <- i
56+
fmt.Printf("Filter: send to out channel: %d\n", i)
57+
}
58+
}
59+
}
60+
61+
func sieve2() {
62+
ch := make(chan int)
63+
go generate2(ch)
64+
for i := 0; i < 3; i++ {
65+
prime := <-ch
66+
fmt.Printf("main: receive from in channel: %d\n", prime)
67+
out := make(chan int)
68+
go filter2(ch, out, prime)
69+
ch = out
70+
}
71+
}

chapter2/sources/gofmt_demo.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package main
44
import "fmt"
55

66
func main() {
7+
s := []int{}
78
fmt.Println(s[3:len(s)])
89
n, err := s.r.Read(s.buf[3:len(s.buf)])
910
reverseLabels = append(reverseLabels, domain[3:len(domain)])
+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
package main
2+
3+
import "testing"
4+
5+
const length = 100000
6+
7+
type S5 struct {
8+
i1, i2, i3, i4, i5 int
9+
}
10+
11+
type S4 struct {
12+
i1, i2, i3, i4 int
13+
}
14+
15+
type S3 struct {
16+
i1, i2, i3 int
17+
}
18+
19+
type S2 struct {
20+
i1, i2 int
21+
}
22+
23+
type S1 struct {
24+
i1 int
25+
}
26+
27+
func BenchmarkForLoopLargeArrayWithStructU5Element(b *testing.B) {
28+
b.ReportAllocs()
29+
for i := 0; i < b.N; i++ {
30+
var a [length]S5
31+
for j := 0; j < length; j++ {
32+
a[j] = S5{i1: j}
33+
}
34+
}
35+
}
36+
37+
func BenchmarkForLoopLargeArrayWithStructU4Element(b *testing.B) {
38+
b.ReportAllocs()
39+
for i := 0; i < b.N; i++ {
40+
var a [length]S4
41+
for j := 0; j < length; j++ {
42+
a[j] = S4{i1: j}
43+
}
44+
}
45+
}
46+
47+
func BenchmarkForLoopLargeArrayWithStructU3Element(b *testing.B) {
48+
b.ReportAllocs()
49+
for i := 0; i < b.N; i++ {
50+
var a [length]S3
51+
for j := 0; j < length; j++ {
52+
a[j] = S3{i1: j}
53+
}
54+
}
55+
}
56+
57+
func BenchmarkForLoopLargeArrayWithStructU2Element(b *testing.B) {
58+
b.ReportAllocs()
59+
for i := 0; i < b.N; i++ {
60+
var a [length]S2
61+
for j := 0; j < length; j++ {
62+
a[j] = S2{i1: j}
63+
}
64+
}
65+
}
66+
67+
func BenchmarkForLoopLargeArrayWithStructU1Element(b *testing.B) {
68+
b.ReportAllocs()
69+
var a [length]S1
70+
for i := 0; i < b.N; i++ {
71+
for j := 0; j < length; j++ {
72+
a[j] = S1{i1: j}
73+
}
74+
}
75+
}
76+
77+
func BenchmarkASForLoopLargeArrayWithStructU5Element(b *testing.B) {
78+
b.ReportAllocs()
79+
var a [length]S5
80+
for i := 0; i < b.N; i++ {
81+
for j := 0; j < length; j++ {
82+
a[j].i1 = j
83+
}
84+
}
85+
}
86+
87+
func BenchmarkASForLoopLargeSliceWithStructU5Element(b *testing.B) {
88+
b.ReportAllocs()
89+
a := make([]S5, length)
90+
for i := 0; i < b.N; i++ {
91+
for j := 0; j < length; j++ {
92+
// a[j].i1 = j
93+
a[j].i1 = j
94+
}
95+
}
96+
}
97+
98+
func BenchmarkASForLoopLargeSliceWithStructU5Element2(b *testing.B) {
99+
b.ReportAllocs()
100+
a := make([]S5, length)
101+
for i := 0; i < b.N; i++ {
102+
for j := 0; j < length; j++ {
103+
// for j, _ := range a {
104+
a[j].i1 = j
105+
// a = append(a, a[j])
106+
}
107+
}
108+
}
109+
110+
func BenchmarkASForRangeLargeArrayWithStructU5Element(b *testing.B) {
111+
b.ReportAllocs()
112+
var a [length]S5
113+
for i := 0; i < b.N; i++ {
114+
for j, _ := range a {
115+
// a[j] = S5{i1: j}
116+
a[j].i1 = j
117+
}
118+
}
119+
}
120+
121+
func BenchmarkASForRangeLargeSliceWithStructU5Element(b *testing.B) {
122+
b.ReportAllocs()
123+
a := make([]S5, length)
124+
for i := 0; i < b.N; i++ {
125+
for j, _ := range a {
126+
// a[j] = S5{i1: j}
127+
a[j].i1 = j
128+
}
129+
}
130+
}
131+
132+
func BenchmarkASForRangeLargeSliceWithStructU5Element2(b *testing.B) {
133+
b.ReportAllocs()
134+
a := make([]S5, 0, length)
135+
for i := 0; i < b.N; i++ {
136+
for j, _ := range a {
137+
// a[j] = S5{i1: j}
138+
a[j].i1 = j
139+
}
140+
}
141+
}
142+
143+
func TestForLoopAndForRange(t *testing.T) {
144+
a := make([]S5, 0, length)
145+
t.Log(a, len(a))
146+
for j, _ := range a {
147+
t.Log(j)
148+
// for j := 0; j < length; j++ {
149+
a[j].i1 = j
150+
}
151+
}
152+
153+
func BenchmarkASForRangeLargeArrayWithStructU4Element(b *testing.B) {
154+
b.ReportAllocs()
155+
for i := 0; i < b.N; i++ {
156+
var a [length]S4
157+
for j, _ := range a {
158+
a[j] = S4{i1: j}
159+
}
160+
}
161+
}
162+
163+
func BenchmarkForRangeLargeArrayWithStructU3Element(b *testing.B) {
164+
b.ReportAllocs()
165+
for i := 0; i < b.N; i++ {
166+
var a [length]S3
167+
for j, _ := range a {
168+
a[j] = S3{i1: j}
169+
}
170+
}
171+
}
172+
173+
func BenchmarkForRangeLargeArrayWithStructU2Element(b *testing.B) {
174+
b.ReportAllocs()
175+
for i := 0; i < b.N; i++ {
176+
var a [length]S2
177+
for j, _ := range a {
178+
a[j] = S2{i1: j}
179+
}
180+
}
181+
}
182+
183+
func BenchmarkForRangeLargeArrayWithStructU1Element(b *testing.B) {
184+
b.ReportAllocs()
185+
for i := 0; i < b.N; i++ {
186+
var a [length]S1
187+
for j, _ := range a {
188+
a[j] = S1{i1: j}
189+
}
190+
}
191+
}
192+
193+
func BenchmarkForLoopLargeArrayWithIntElement(b *testing.B) {
194+
b.ReportAllocs()
195+
for i := 0; i < b.N; i++ {
196+
var a [length]int
197+
for j := 0; j < length; j++ {
198+
a[j] = j
199+
}
200+
}
201+
}
202+
203+
func BenchmarkForRangeLargeArrayWithIntElement(b *testing.B) {
204+
b.ReportAllocs()
205+
for i := 0; i < b.N; i++ {
206+
var a [length]int
207+
for j, _ := range a {
208+
a[j] = j
209+
}
210+
}
211+
}

chapter3/sources/control_structure_idiom_1.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,29 @@ func demo2() {
3131
time.Sleep(time.Second * 10)
3232
}
3333

34+
func showForLoopLocalVariableReuse() {
35+
s := []int{1, 2, 3}
36+
for i, v := range s {
37+
go func() {
38+
fmt.Println(i, v)
39+
}()
40+
}
41+
time.Sleep(time.Second)
42+
}
43+
44+
func showForLoopLocalVariableReuse2() {
45+
s := []int{1, 2, 3}
46+
for i, v := range s {
47+
go func(i, v int) {
48+
fmt.Println(i, v)
49+
}(i, v)
50+
}
51+
time.Sleep(time.Second)
52+
}
53+
3454
func main() {
35-
demo1()
36-
demo2()
55+
showForLoopLocalVariableReuse()
56+
showForLoopLocalVariableReuse2()
57+
// demo1()
58+
// demo2()
3759
}

0 commit comments

Comments
 (0)