conn is probably nil if there is an error. Call Close only if there is no 
error.

conn, err := net.Dial(...)
if err != nil {
    // handle error
}
defer conn.Close()

Am Sonntag, 11. Dezember 2016 00:28:36 UTC+1 schrieb Stannis Kozlov:
>
> I'm trying to write simple port scanner and using "net" to check port 
> availability:
> func sock() {
>   conn, err := net.Dial("tcp", "192.168.0.1:9991")
>   if err != nil {
>     fmt.Printf("\n!!!:: %v", err)
>     conn.Close() 
>
> Debug return error:
> !!!:: dial tcp 192.168.0.1:9991: getsockopt: connection refusedpanic: 
> runtime error: invalid memory address or nil pointer dereference 
> [signal 0xb code=0x1 addr=0x20 pc=0x401186] 
>
> goroutine 1 [running]: 
> panic(0x564e00, 0xc82000a140) 
>        /usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6 
> main.sock() 
>        /home/m/go/ps.go:16 +0x186 
> main.main() 
>        /home/m/go/ps.go:70 +0x620 
> exit status 2
>
>
> It looks like "refused" calls runtime error and app crash in panic. How 
> can I work with errors to keep my app working?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to