-
-
Save azhuox/a67a6e1cea706a787e017f0786479e13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "io" | |
import "bytes" | |
import "fmt" | |
func main() { | |
var w io.Writer | |
fmt.Println(w == nil) // True | |
var b *bytes.Buffer | |
w = b | |
fmt.Println(w == nil) // false | |
write(w) // panic: runtime error: invalid memory address or nil pointer dereference | |
} | |
// If out is non-nil, output will be written to it. | |
// | |
func write(out io.Writer) { | |
// ...do something... | |
if out != nil { // This guard is not secure enough | |
out.Write([]byte("done!\n")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment