nil pointer
March 17, 2017
To check that x
implements y
, example:
package main
import (
"fmt"
"io"
"os"
)
var _ io.ReadCloser = (*os.File)(nil)
func main() {
fmt.Println("Hello, playground")
}
https://play.golang.org/p/hoi5_GQEpD
To check that os.File
is an io.ReadCloser
:
var _ io.ReadCloser = (*os.File)(nil)
package main
import (
"fmt"
"io"
"os"
)
var _ io.ReadCloser = (*os.Process)(nil)
func main() {
fmt.Println("Hello, playground")
}
https://play.golang.org/p/7uHmjn3ggT
The error in this case would be:
main.go:9: cannot use (*os.Process)(nil) (type *os.Process) as type io.ReadCloser in assignment:
*os.Process does not implement io.ReadCloser (missing Close method)