-
Notifications
You must be signed in to change notification settings - Fork 127
/
lesson5.slide
155 lines (122 loc) · 3.84 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
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
Testing, CGo and tools
Lesson 5
30 May 2024
Tags: golang, go
Jakub Čajka <[email protected]>
Red Hat, Inc.
https://github.com/RedHatOfficial/GoCourse
@RedHat
* Sources
- [[https://github.com/RedHatOfficial/GoCourse]]
.image ./common/qr_address.png
* 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`)
* Assertions in Go
- `assert` is not keyword in Go
- but assert-like functions and methods exist
- third party libraries
* Assertions in Go (factorial)
.code testing/assertions/assertions1/factorial.go
* Assertions in Go (factorial)
.code testing/assertions/assertions1/factorial_test.go
* More assertions-like functions
.code testing/assertions/assertions2/factorial_test.go
* "Dot import" usage
.code testing/assertions/assertions3/factorial_test.go
* 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
* 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
- formats code to the common coding standard
- go fix
- forward ports your code to latest release version of Go
* External tools
- abcgo
- gosec
- gocyclo
- goconst
- (go)errcheck
- gocritic
- golangci-lint
* 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
- go list -u -m -json all
- go list -u -m -json all | go-mod-outdated -update
* 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