-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathabout_methods.go
68 lines (52 loc) · 1.3 KB
/
about_methods.go
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
package main
type Int int
func (t Int) Add(v Int) Int {
return Int(int(t) + int(v))
}
func AGlobalFunction(a, b int) int {
return a + b
}
func FunctionWithMultipleReturnTypes() (int, string) {
return 3, "hello"
}
func FunctionWithNamedReturnValues() (a int, b string) {
a = 4
b = "bye"
return
}
func TestFunctionWithNamedReturnValues(t *T) {
intValue, stringValue := FunctionWithNamedReturnValues()
t.AssertEqualInt(Int__, intValue)
t.AssertTrue(String__ == stringValue)
}
func TestFunctionWithMultipleReturnTypes(t *T) {
intValue, stringValue := FunctionWithMultipleReturnTypes()
t.AssertEqualInt(Int__, intValue)
t.AssertTrue(String__ == stringValue)
}
func TestCallingAGlobalFunction(t *T) {
result := AGlobalFunction(3, 4)
t.AssertEqualInt(Int__, int(result))
}
func TestEveryTypeCanHaveMethods(t *T) {
v := Int(4)
result := v.Add(3)
t.AssertEqualInt(Int__, int(result))
}
type Interface interface {
ReturnValue() int
}
type Implementation struct {
}
// The declaration of this function makes
// only a pointer to Implementation satisfy Interface.
func (t *Implementation) ReturnValue() int {
return 1
}
func TestInterfaces(t *T) {
var i Interface
v := Implementation{}
i = &v //Only a pointer to Implementation satisfies Interface.
value := i.ReturnValue()
t.AssertEquals(2, value)
}