Golang : How to flush a channel before the end of program?
Problem :
Your program that uses channels and go routines did not print out all the expected result. Upon investigation, it seems that the data in channels were not flushed out before the program termination. What can be done to flush data from channel before program termination?
Solution :
There is no build in method to "flush" data out from channel. You can either use WaitGroup to pause the program termination until all channels data are printed out or program your function to indicate if they are done (i.e goroutine completed ) before moving on to next process or terminate the program.
See examples at :
https://www.socketloop.com/tutorials/golang-wait-and-sync-waitgroup-example
https://www.socketloop.com/tutorials/golang-close-channel-after-ticker-stopped-example
See also : Golang : Close channel after ticker stopped example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.8k Golang : Shuffle array of list
+5.9k Golang : Compound interest over time example
+11.6k Golang : GTK Input dialog box examples
+7.3k Golang : Get YouTube playlist
+5.9k PageSpeed : Clear or flush cache on web server
+10k Golang : Compare files modify date example
+9.6k Golang : Resumable upload to Google Drive(RESTful) example
+8.6k Golang : Find duplicate files with filepath.Walk
+32.9k Delete a directory in Go
+7.5k Golang : Command line ticker to show work in progress
+6.9k Golang : Takes a plural word and makes it singular
+4.6k Golang : How to pass data between controllers with JSON Web Token