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
+6.6k Golang : Map within a map example
+6.8k Golang : Humanize and Titleize functions
+17.7k Convert JSON to CSV in Golang
+6.6k Golang : Calculate diameter, circumference, area, sphere surface and volume
+15.3k Golang : Get HTTP protocol version example
+18.7k Golang : Get download file size
+7.4k Golang : Calculate how many weeks left to go in a given year
+9.2k Golang : Gonum standard normal random numbers example
+5.1k Google : Block or disable caching of your website content
+7.2k Golang : Gorrila mux.Vars() function example
+8.2k Golang : Tell color name with OpenCV example
+13k Python : Convert IPv6 address to decimal and back to IPv6