forked from RedHatOfficial/GoCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson5.slide
119 lines (96 loc) · 3.08 KB
/
lesson5.slide
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
Testing, CGo and tools
Lesson 5
11 Oct 2019
Tags: golang, go
Jakub Čajka <[email protected]>
Red Hat, Inc.
https://github.com/RedHatOfficial/GoCourse
@RedHat
* Gophers
#The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
#Source https://golang.org/doc/gopher/fiveyears.jpg
#The design and this image is licensed under the Creative Commons 3.0 Attributions license.
.image ./common/fiveyears.jpg _ 900
* Testing and Benchmarking in Go
- Unit test "framework" integral to the Go command
- go test
- (Micro)Benchmark "framework" part of it too
- go test -bench .
- package testing
- code needs to be "vetable", pass go vet check
* Tests
- in _test.go files
- all Test*(t *testing.T) functions get executed
* Test main
- func TestMain(m *testing.M)
- m.Run
- provides ability to do setup, passing CLI flags,...
- execute tests that don't fit the basic framework, etc.
- checkout misc/cgo GC tests
* Benchmarks
- in _test.go files
- all Benchmark*(b *testing.B) functions get executed
- can benchmark not only performance(-bench), but memory usage(-benchmem)
* Go test tips&tricks
- you can use build tags and/or short mode
- good practice auxiliary data in testdata sub-directory/package, ignored by go build
* Example 1
.code lesson5/cgo-math-test-bench/cgo-math_test.go /T1/,/T1/
* Example 2
.code lesson5/cgo-math-test-bench/cgo-math_test.go /T2/,/T2/
* Example 3
.code lesson5/cgo-math-test-bench/cgo-math_test.go /benchmark/,/benchmark/
* CGo?
- C and Go, Cgo, but not a Go
- allows interoperability between Go and C code
- calling C function in Go and Go functions in C
- "FFI"
- "C" "meta" package
* CGo
- has overhead, look for solution in Go
- C code can't hold pointers in to the Go world
- ^^ due to the copying nature of the GC
- need to manually free malloc'ed(heap) C memory
- string, arrays and such
* Example C in Go inline
.code lesson5/cgo-inlinec/cgo-inlinec.go
* Example C in Go separate files
.code lesson5/cgo-independent/cgo-independent.go
* Example memory management with CGo and C strings
.code lesson5/cgo-strings/cgo-string.go /memory management/,/memory management end/
* Example Go in C
- don't
- again don't
- look at the CGo docs
- BEWARE! it has extreme overhead
* Builtin tools
- go vet
- reports suspicions code construct, possible bugs
- go fmt
- formates code to the common coding standard
- go fix
- forward ports your code to latest release version of Go
* Dependency management
* The Past
- go get and GOPATH
- vendoring aka bundling
- plethora of tools, godep, govendor, glide
* The Present
- Go modules, go mod
- local cache instead of GOPATH
- go mod
- go.mod storing informations for module
- init, vendor, download
- go get bar@...
- go list -m
* Proxies and sumdbs
- Proxies facilitates fetches, instead of direct
- SumDBs hashes of repos for corresponding versions
* The future?
-?
#last slide
* More Gophers
#The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
#Source https://golang.org/doc/gopher/bumper.png
#The design and this image is licensed under the Creative Commons 3.0 Attributions license.
.image ./common/bumper.png _ 900