29/Mar 2015
3 min. read
Overview A common problem people new to Go tend to run into is managing goroutines. Starting a goroutine is easy but how do you guarantee it closes gracefully when you are ready to exit? All code for this example lives here. First, lets create a struct that will manage our goroutine. In this case it’s just going to be one but there are no restrictions here. //App is an example struct that will run goroutines type App struct { wg *sync.WaitGroup //allow us to wait for close done chan bool Data chan string } func NewApp() (app *App) { app = &App{ wg: &sync.WaitGroup{}, done: make(chan bool, 1), Data: make(chan string, 100), } return app With this we can now add a few methods to start and run our goroutine.