-
Notifications
You must be signed in to change notification settings - Fork 1
/
04_first_go_program.slide
92 lines (66 loc) · 1.94 KB
/
04_first_go_program.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
# My first go program
Maximilian Wurzer
Advisory IT Architect, IBM Deutschland
## Toolbox
- Go to
.link https://play.golang.org/
- Hack away
- Questions? - Use the Webex Chat
## Exercise User
- Create a public `struct` *User* with the fields
- id (`uint32`)
- name (`string`)
- surname (`string`)
- gopher (`bool`)
- Create your personal user
- `id` ← `1`
- `name` and `surname` ← add your name
- `gopher` ← obviously, `true`
- Print the result to STDOUT
## Solution User
## Result User - Solution 1
.iframe https://play.golang.org/p/j0EuuwQuigJ 550 900
//.play -edit presentation_code/firstGoProgramSolution1.go
## Result User - Solution 2
.iframe https://play.golang.org/p/uedHAieCYvT 550 900
//.play -edit presentation_code/firstGoProgramSolution2.go
## Exercise Sort
- Go comes with a sort interface which can be adapted
- `Len`, `Less`, and `Swap`
```
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with index i
// must sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}
```
- Create a type which implements the `sort.Interface` based on a User slice
- Sort the users by ID
## Solution Sort
## Result Sort
.iframe https://play.golang.org/p/6cedRdP83V- 750 900
//.play -edit firstGoProgramSortSolution.go
## Exercise JSON
- Create a public `struct` *User* with the fields (capital letters are important)
- ID (`uint32`)
- Name (`string`)
- Surname (`string`)
- gopher (`bool`)
- Create a user and encode the user to a JSON with
```
b, err := json.Marshal(m)
```
- Print the JSON (you will only see the fields `ID`, `Name` and `Surname`)
- Decode the JSON with
```
var m User
err := json.Unmarshal(b, &m)
```
and print the Name
## Solution JSON
## Result JSON
.iframe https://play.golang.org/p/96YSbccknz8 750 900