-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncmethodsinterfaces.slide
More file actions
137 lines (72 loc) · 3.68 KB
/
funcmethodsinterfaces.slide
File metadata and controls
137 lines (72 loc) · 3.68 KB
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
Functions, Methods and Interfaces
Brian Ketelsen
me@brianketelsen.com
@bketelsen
* Functions, Methods and Interfaces
* Functions
Functions are declared with the "func" keyword.
Functions have a name, optional input parameters, and optional return values.
- Function Examples
cd $GOPATH/src/github.com/gophertrain/material/funcmethodsinterfaces/demos/funcs
go run main.go
Functions are first-class types in Go. You can assign a function to a variable, you can pass functions as parameters.
- Function Values
cd $GOPATH/src/github.com/gophertrain/material/funcmethodsinterfaces/demos/funcvalues
go run main.go
* Methods
Methods are syntactic sugar for a function with a type as the first parameter.
WHAT?
func ChangeEmail(u *User, newEmail string) { ... } // Ugly
func (u *User) ChangeEmail(newEmail string) { ... } // Clear
These are equivalent in functionality, but one of them is much more clear.
* Methods
Methods can be declared on any named type. Use a pointer receiver when you want to modify the existing type. Use a value receiver when you don't need to modify the type.
- Method Example
cd $GOPATH/src/github.com/gophertrain/material/funcmethodsinterfaces/demos/method
go run main.go
Methods are First Class Citizens in Go
That means you can create variables of type method, assign to them, and operate on those variables.
- First Class Methods
cd $GOPATH/src/github.com/gophertrain/material/funcmethodsinterfaces/demos/firstmethod
go run main.go
* Interfaces
Interfaces allow you to specify BEHAVIOR.
If something can do this, then it can be used here.
Interfaces are types. They are declared as types.
Interfaces usually have a very small number of Methods, 1 or 2 is most common.
The larger the interface, the weaker the abstraction. -- Rob Pike
Interface names try to describe the action.
* Interface Examples
Stringer - a type that has a method that returns a string
io.Writer - a type that has a method that writes to a buffer
io.ReadCloser - a type that has a method that reads from a stream and closes it when done
* Creating Good Interfaces
Good interfaces define a very small set of specific actions:
- Writing bytes to a buffer (io.Writer)
- Returning a String representing a type (fmt.Stringer)
* Standard Library Interfaces
Examples of Interfaces in Go's standard library:
.link https://golang.org/pkg/database/sql/driver/#Conn db/sql: connection interface
.link https://golang.org/pkg/encoding/ encoding: Marshaler Interfaces
.link https://golang.org/pkg/net/http/#Handler net/http: HTTP Handler Interface
By these patterns, you can see that interfaces are intended to represent a small set of behaviors.
* Interfaces in Practice
- Interface Example
cd $GOPATH/src/github.com/gophertrain/material/funcmethodsinterfaces/demos/interfaces
go run main.go
* The Empty Interface
All the interfaces we've seen up to now have declared one or more functions. Interfaces don't have to declare any functions, though.
If you declare an interface with an empty set of functions, then any type will satisfy that interface.
In Go we use the empty interface to represent "anything".
- Empty Interface Example
cd $GOPATH/src/github.com/gophertrain/material/funcmethodsinterfaces/demos/empty
go run main.go
* Type Assertions
That's pretty powerful. You can pass any type around without losing the type safety. But when you receive an "interface{}" how do you know what to do with it?
- Type Assertion Example
cd $GOPATH/src/github.com/gophertrain/material/funcmethodsinterfaces/demos/assert
go run main.go
* Exercise
Fix the last example to recognize and print the float value.
cd $GOPATH/src/github.com/gophertrain/material/funcmethodsinterfaces/exercises/assert
go run main.go