| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
) |
| 7 |
|
| 8 |
type Nil interface { |
| 9 |
String() string |
| 10 |
} |
| 11 |
|
| 12 |
func MakeNil() Nil { |
| 13 |
var n *NilStruct |
| 14 |
return n |
| 15 |
} |
| 16 |
|
| 17 |
type NilStruct struct { |
| 18 |
Data string |
| 19 |
} |
| 20 |
|
| 21 |
func (n *NilStruct) String() string { |
| 22 |
return n.Data |
| 23 |
} |
| 24 |
|
| 25 |
func main() { |
| 26 |
var n *NilStruct |
| 27 |
fmt.Printf("%t %#v %s %t\n", |
| 28 |
n == nil, |
| 29 |
n, |
| 30 |
reflect.ValueOf(n).Kind(), |
| 31 |
reflect.ValueOf(n).IsNil()) |
| 32 |
n2 := MakeNil() |
| 33 |
fmt.Printf("%t %#v %s %t\n", |
| 34 |
n2 == nil, //@ diag(`this comparison is never true`) |
| 35 |
n2, |
| 36 |
reflect.ValueOf(n2).Kind(), |
| 37 |
reflect.ValueOf(n2).IsNil()) |
| 38 |
fmt.Println(n2.String()) |
| 39 |
} |