Undefined values: Difference between revisions
→{{header|Go}}: needed to test different conditions after language changes
(Move BASIC and part of ActionScript from Null object. Also stop omit C.) |
(→{{header|Go}}: needed to test different conditions after language changes) |
||
Line 202:
For this task, I demonstrate,
# How certain attempts to use nil objects cause panics.
# How to initialize objects of these types.
# Successful (non panicking) use of initialized objects.
Line 225:
// initialize objects
s = make([]int, 1)
p = &s[0]
f = func() { fmt.Println("function call") }
i = user(0)
m = make(map[int]int)
c = make(chan int, 1)
Line 280:
case nil:
fmt.Println("i is nil interface")
case interface {
m()
}:
fmt.Println("i has method m")
}
// assert type with method and then call method
i.(interface {
m()
}).m()
}
func tryMap() {
defer reportPanic()
m[0] = 0
fmt.Println("m[0] =", m[0])
}
Line 295 ⟶ 300:
func tryChannel() {
defer reportPanic()
close(c
fmt.Println("
}</lang>
Output:
Line 305 ⟶ 310:
panic: runtime error: invalid memory address or nil pointer dereference
i is nil interface
panic: interface conversion: interface is nil, not interface { main.m() }
panic: runtime error:
panic: runtime error:
Exercise objects after initialization:
Line 316 ⟶ 321:
method call
m[0] = 0
channel closed
</pre>
|