forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1.go
66 lines (53 loc) · 1.86 KB
/
exercise1.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// https://play.golang.org/p/MFPF58SkRs
// Declare an interface named speaker with a method named sayHello. Declare a struct
// named English that represents a person who speaks english and declare a struct named
// Chinese for someone who speaks chinese. Implement the speaker interface for each
// struct using a value receiver and these literal strings "Hello World" and "你好世界".
// Declare a variable of type speaker and assign the _address of_ a value of type English
// and call the method. Do it again for a value of type Chinese.
//
// Add a new function named sayHello that accepts a value of type speaker.
// Implement that function to call the sayHello method on the interface value. Then create
// new values of each type and use the function.
package main
import "fmt"
// speaker implements the voice of anyone.
type speaker interface {
sayHello()
}
// english represents an english speaking person.
type english struct{}
// sayHello implements the speaker interface.
func (english) sayHello() {
fmt.Println("Hello World")
}
// chinese represents a chinese speaking person.
type chinese struct{}
// sayHello implements the speaker interface.
func (chinese) sayHello() {
fmt.Println("你好世界")
}
// main is the entry point for the application.
func main() {
// Declare a variable of the interface type.
var sp speaker
// Assign a value to the interface type and
// call the interface method.
var e english
sp = e
sp.sayHello()
// Assign a different value to the interface type and
// call the interface method.
var c chinese
sp = c
sp.sayHello()
// Create new values and call the function.
sayHello(new(english))
sayHello(new(chinese))
}
// SatHello abstracts speaking functionality.
func sayHello(sp speaker) {
sp.sayHello()
}