assert.go
965 Bytes
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
// RUN: llgo -o %t %s
// RUN: %t 2>&1 | FileCheck %s
// CHECK: x is nil
// CHECK-NEXT: i2v: 123456
// CHECK-NEXT: !
// CHECK-NEXT: (*X).F1: 123456
package main
type X struct{ x int }
func (x *X) F1() { println("(*X).F1:", x.x) }
func (x *X) F2() { println("(*X).F2") }
type I interface {
F1()
F2()
}
func main() {
var x interface{}
// x is nil. Let's make sure an assertion on it
// won't cause a panic.
if x, ok := x.(int32); ok {
println("i2v:", x)
}
if x == nil {
println("x is nil")
}
x = int32(123456)
// Let's try an interface-to-value assertion.
if x, ok := x.(int32); ok {
println("i2v:", x)
}
if x, ok := x.(int64); ok {
println("i2v:", x)
}
// This will fail the assertion.
if i, ok := x.(I); ok {
i.F1()
_ = i
} else {
println("!")
}
// Assign an *X, which should pass the assertion.
x_ := new(X)
x_.x = 123456
x = x_ //&X{x: 123456}
if i, ok := x.(I); ok {
i.F1()
_ = i
} else {
println("!")
}
}