-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_tools.slide
115 lines (85 loc) · 3.38 KB
/
03_tools.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
# Go Tools
Maximilian Wurzer
Advisory IT Architect, IBM Deutschland
## The 'go' utility (1/3)
Compiler, package-manager, formatter, linter, ... all in a **single tool**:
```
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
```
## The 'go' utility (2/3)
**`go run`**: Compile and run a program ad-hoc
- `go run hello.go`
**`go build`**: Compile or cross-compile program
- `go build hello.go`
- `GOOS=windows GOARCH=amd64 go build hello.go`
**`go test`**: Run tests
- `go test ./...`
**`go get`**: Download a dependency of a module and install them
- `go get -u github.com/google/uuid`
## The 'go' utility (3/3)
**`go doc`**: Get the docs for a method
- `go doc http.ListenAndServe`
```bash
package http // import "net/http"
func ListenAndServe(addr string, handler Handler) error
ListenAndServe listens on the TCP network address addr and then calls Serve
with handler to handle requests on incoming connections. Accepted
connections are configured to enable TCP keep-alives.
The handler is typically nil, in which case the DefaultServeMux is used.
ListenAndServe always returns a non-nil error.
```
**`go mod`**: Interact with modules
- `go mod init`
- `go mod vendor`
//- vendor
//- CGO & FFI
//- ...
## Go-Playground
- run small code samples
- share code samples (e.g. Stack Overflow questions: show an example)
.link https://play.golang.org/
.iframe https://play.golang.org/p/B5ZShtSizEy 300 800
## Package-Management - Go has a unique approach to package management
- Package management is entirely source based, not artifact based
- Instead of referencing a package in a repository, go references source code
- Entirely git based
- Import statement: `import 'gitlab.com/my/project'`
## Package-Management - Modules
- Modules introduced in 1.12 to enable semantic versioning
- Modules still reference source code repositories
- Semantic versioning is enforced
- go.mod file lists dependencies, go.sum maintains hash sums of dependencies
- Sample `go.mod` file:
```
module example.com/my/thing
go 1.12
require example.com/other/thing v1.0.2
require example.com/new/thing/v2 v2.3.4
exclude example.com/old/thing v1.2.3
replace example.com/bad/thing v1.4.5 => example.com/good/thing v1.4.5
```
## Online resources
- Go-Website: [go.dev](https://go.dev) and [golang.org](https://golang.org)
- Blog: [blog.golang.org](https://blog.golang.org)
- Package search: [pkg.go.dev](https://pkg.go.dev/)
- Github
- Stack Overflow
- Reddit
- Libraries (new, top, growing): [https://libs.garden/go](https://libs.garden/go)
- also: [github.com/avelino/awesome-go](https://github.com/avelino/awesome-go)