This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path09-struct.slide
121 lines (62 loc) · 3.1 KB
/
09-struct.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
Defined types and structs in Go
22 Feb 2017
Tags: edmontongo, golang, workshop
* Defined types
Types help us organize data.
* Adding meaning to a basic type
.play -edit 09-struct/conflation.go /START OMIT/,/END OMIT$/
* Enumerated constants
.play -edit 09-struct/enum.go /START OMIT/,/END OMIT$/
* Composite types
.play -edit 09-struct/composite.go /START OMIT/,/END OMIT$/
* Type declarations
- Create a new type, not an alias.
.code 09-struct/conflation.go /START DISTINCT OMIT/,/END DISTINCT OMIT/
- Operations (+, *, [], etc) of the new type are determined by the underlying type.
.play -edit 09-struct/definition.go /START OMIT/,/END OMIT$/
* Explicit type conversions
Conversions are possible between:
- same underlying type
- pointers to same underlying type
- numeric types
- string and byte or rune slices
.play -edit 09-struct/conversion.go /START OMIT/,/END OMIT$/
* Struct literals
- Fields in struct literals can be given positionally or with names.
- Struct literals are addressable using &.
- Can be nested.
.play -edit 09-struct/literals.go /START OMIT/,/END OMIT$/
* Zero values
Missing fields are initialized to zero. If fields are given positionally, they all must be given.
.play -edit 09-struct/missing.go /START OMIT/,/END OMIT$/
Useful zero values:
.play -edit 09-struct/zero.go /START OMIT/,/END OMIT$/
* Structs are passed by value
A shallow copy is made. To mutate a struct argument, pass a pointer to it. The dot operator automatically dereferences pointers.
.play -edit 09-struct/pass.go /START OMIT/,/END OMIT$/
* Comparing structs
Structs of the same type are comparable if all their fields are comparable.
.play -edit 09-struct/cmp.go /START OMIT/,/END OMIT$/
* Nesting structs
As a field:
.play -edit 09-struct/nest.go /START OMIT/,/END OMIT$/
Including another struct's fields using embedding:
.play -edit 09-struct/embed.go /START OMIT/,/END OMIT$/
* Exercises
Make a simple model of bank account using structs and functions.
- Create an enumerated constant for a bank account type: Checking or Savings.
- Create a struct for someone's bank account, including a name, account balance, and account type.
- Write a function which takes the struct as an argument and prints information about the account.
- Write the functions Withdraw and Deposit which take the struct and a positive amount and update the account balance.
* Solution
- Create an enumerated constant for a bank account type: Checking or Savings.
- Create a struct for someone's bank account, including a name, account balance, and account type.
.play -edit 09-struct/solution.go /START TYPE/,/END TYPE/
* Solution
- Write a function which takes the struct as an argument and prints information about the account.
.play -edit 09-struct/solution.go /START PRINT/,/END PRINT/
.play -edit 09-struct/solution.go /START PRINT MAIN/,/END PRINT MAIN/
* Solution
- Write the functions Withdraw and Deposit which take the struct and a positive amount and update the account balance.
.play -edit 09-struct/solution.go /START WITHDRAW/,/END WITHDRAW/
.play -edit 09-struct/solution.go /START WITHDRAW MAIN/,/END WITHDRAW MAIN/