I recently wrote a Go func that starts off by opening a network listening connection and defers closing that connection.
connection, err := icmp.ListenPacket(...)
if err != nil {
panic(err)
}
defer connection.Close()
The defer'd connection.Close()
returns an error, but how to capture and do something with that error if it occurs?
A solution was not so obvious to me. Thankfully I stumbled upon a very comprehensive and detailed answer.A bit of overkill (for my purposes) that led me to this simple implementation.
connection, err := icmp.ListenPacket(...)
if err != nil {
panic(err)
}
defer func() {
err = connection.Close()
if err != nil {
fmt.Printf("%v\n", err)
}
}()
Thank you most kindly pocketgophers. Life is good.
Like this article?
0
Pragmatic programmer. Consilient alchemist. Serial hobbyist. Pro geek. Miniature tabletop gamer.