Break out of 3rd party goroutine that has an infinite loop

Issue

I’m using this to receive SNMP traps: https://github.com/soniah/gosnmp
Now, lets say I want to programmatically break out of the (taken from here):

err := tl.Listen(“0.0.0.0:9162”)

What are my best approaches to this?

I’m somewhat new to Golang and didnt find a way to break out of a goroutine that I have no way of modifying (“3rd party”).

Thanks,

Solution

Short answer: You can’t. There’s no way to kill a goroutine (short of killing the entire program) from outside the goroutine.

Long answer: A goroutine can listen for some sort of “terminate” signal (via channels, signals, or any other mechanism). But ultimately, the goroutine must terminate from within.

Looking at the library in your example, it appears this functionality is not provided.

Answered By – Flimzy

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.