-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics2.slide
More file actions
187 lines (111 loc) · 3.8 KB
/
basics2.slide
File metadata and controls
187 lines (111 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
Go Basics 2
Tags: golang, workshop, avocode
Daniel Hodan
Avocode
danielhodan@avocode.com
http://avocode.com/
*
.image images/light-logo.svg
.image images/gopher.png
* Testing
Lightweight test framework included in package `testing`.
File with a name ending in `_test.go`
> ls github.com/czertbytes/mypkg
mypkg.go mypkg_test.go
That contains functions named `TestXXX` with signature func `(t` `*testing.T)`.
func TestMyFunc(t *testing.T) {
...
}
Run tests with `go` `test`.
> go test github.com/czertbytes/mypkg
ok github.com/czertbytes/mypkg 0.085s
* Testing example
.code -edit test/mypkg_test.go /START OMIT/,/END OMIT/
* Benchmarking
Lightweight test framework included in package `testing`.
File with a name ending in `_test.go`
> ls github.com/czertbytes/mypkg
mypkg.go mypkg_test.go
That contains functions named `BenchmarkXXX` with signature func `(b` `*testing.B)`.
func BenchmarkMyFunc(b *testing.B) {
...
}
Run tests with `go` `test` `-bench`
> go test -bench github.com/czertbytes/mypkg
BenchmarkSHA512Hex-4 30000 41735 ns/op
PASS
ok github.com/czertbytes/mypkg 1.669s
* Benchmarking example
.code -edit benchmark/mypkg_test.go /START OMIT/,/END OMIT/
* Remote packages
If you need more than standard library use `go` `get`
> go get github.com/czertbytes/mypkg
- Download package in $GOPATH/src
- Compile in $GOPATH/pkg
- Install binary in $GOPATH/bin
Import as any other package in you $GOPATH
import "github.com/czertbytes/mypkg"
* Remote packages
What if I have need more versions of given package in one $GOPATH?
- Multiple $GOPATH's
- Package versioning in URL
- Vendor package locally in `vendor`
* Remote packages
Vendoring with `vendor` folder - import path is the same.
import "github.com/czertbytes/mypkg"
Import order: `vendor`, $GOPATH, $GOROOT
> tree
.
├── foo.go
├── foo_test.go
└── vendor
└── github.com
└── czertbytes
└── mypkg
├── mypkg.go
└── mypkg_test.go
4 directories, 4 files
* Concurrency patterns
* WaitGroup
A `WaitGroup` waits for a collection of goroutines to finish.
var wg sync.WaitGroup
Increase internal counter.
wg.Add(1)
Decrease internal counter.
wg.Done()
Wait blocks until counter is 0.
wg.Wait()
* WaitGroup example
.play waitgroup/waitgroup.go
* Generator
A Generator is function that returns the next value in a sequence each time the function is called.
for next := range generator() {
fmt.Println(next)
}
* Generator example
.play -edit generator/generator.go /START OMIT/,/END OMIT/
* Fan-in
A function can read from multiple inputs and proceed until all are closed by multiplexing the input channels onto a single channel that's closed when all the inputs are closed
.image images/gophermegaphones.jpg
* Fan-in example
.play fanin/fanin.go /START OMIT/,/END OMIT/
* Fan-out
Multiple functions can read from the same channel until that channel is closed.
* Fan-out example
.play fanout/fanout.go /START OMIT/,/END OMIT/
* Context
Context allows passing request scoped values, cancelation signals, and deadlines across API boundaries to all the goroutines involved in handling a request.
With value
ctx := context.WithValue(context.Background(), "key", "value")
With cancel
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
With deadline
ctx, cancel := context.WithDeadline(context.Background(), deadlineTime)
defer cancel()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
* Context example
.play -edit context/context.go /START OMIT/,/END OMIT/
* Live coding
* Summary