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
| type Coder interface {
code()
}
type Gopher struct {
}
func (g Gopher) code() {
}
func main() {
var c Coder
fmt.Println(c == nil) // true
fmt.Printf("c: %T, %v\n", c, c) // c: <nil>, <nil>
var g *Gopher
fmt.Println(g == nil) // true
// declare an interface `g`, type of `g` is still `nil`
fmt.Printf("g: %T, %v\n", g, g) // g: *main.Gopher, <nil>
// assign interface `c` struct type `g`
c = g
fmt.Println(c == nil) // false
fmt.Printf("c: %T, %v\n", c, c) // c: *main.Gopher, <nil>
}
|