-
Notifications
You must be signed in to change notification settings - Fork 0
/
x56.go
60 lines (52 loc) · 995 Bytes
/
x56.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
package main
import (
"fmt"
"reflect"
)
type Pet1 interface {
Name() string
Category() string
}
type Dog1 struct {
name string // 名字。
}
func (dog *Dog1) SetName(name string) {
dog.name = name
}
func (dog Dog1) Name() string {
return dog.name
}
func (dog Dog1) Category() string {
return "dog"
}
func main() {
// 示例1。
var dog1 *Dog1
fmt.Println("The first dog is nil.")
dog2 := dog1
fmt.Println("The second dog is nil.")
var pet Pet1 = dog2
if pet == nil {
fmt.Println("The pet is nil.")
} else {
fmt.Println("The pet is not nil.")
}
fmt.Printf("The type of pet is %T.\n", pet)
fmt.Printf("The type of pet is %s.\n", reflect.TypeOf(pet).String())
fmt.Printf("The type of second dog is %T.\n", dog2)
fmt.Println()
// 示例2。
wrap := func(dog *Dog1) Pet1 {
if dog == nil {
fmt.Println("1")
return nil
}
return dog
}
pet = wrap(dog2)
if pet == nil {
fmt.Println("The pet is nil.")
} else {
fmt.Println("The pet is not nil.")
}
}