Undefined values: Difference between revisions

m
→‎{{header|Go}}: clean up. no substantial code changes.
(Go solution)
m (→‎{{header|Go}}: clean up. no substantial code changes.)
Line 147:
# Successful (non panicking) use of initialized objects.
# One more quirky little feature involving a type switch on a nil interface.
<lang go>package main
 
import ("fmt"
package main
 
import (
"fmt"
)
 
var (
Line 168 ⟶ 164:
status()
 
// initialize objects
s = make([]int, 1)
p = &s[0] // yes, reference element of slice just created
f = func() { fmt.Println("function call") }
i = user(0) // see user defined type just below
m = make(map[int]int)
c = make(chan int, 1)
Line 181 ⟶ 178:
type user int
 
func (u user) m() {
fmt.Println("method call")
}
 
func status() {
nilSlicetrySlice()
nilPointertryPointer()
nilFunctiontryFunction()
nilInterfacetryInterface()
nilMaptryMap()
nilChanneltryChannel()
}
 
 
func reportPanic() {
Line 201 ⟶ 197:
}
 
func nilSlicetrySlice() {
defer reportPanic()
fmt.Println("s[0] =", s[0])
}
 
func nilPointertryPointer() {
defer reportPanic()
fmt.Println("*p =", *p)
}
 
func nilFunctiontryFunction() {
defer reportPanic()
f()
}
 
func nilInterfacetryInterface() {
 
func nilInterface() {
defer reportPanic()
 
Line 234 ⟶ 229:
}
 
func nilMaptryMap() {
defer reportPanic()
fmt.Println("m[0] =", m[0])
}
 
func nilChanneltryChannel() {
defer reportPanic()
c <- 0
fmt.Println("<-c received", <-c)
}</lang>
}
</lang>
Output:
<pre>
1,707

edits