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
+35.8k Golang : Converting a negative number to positive number
+9.6k Golang : Sort and reverse sort a slice of integers
+29.9k Golang : How to verify uploaded file is image or allowed file types
+11.4k How to tell if a binary(executable) file or web application is built with Golang?
+11.6k Golang : Find and draw contours with OpenCV example
+15.3k Golang : How to convert(cast) IP address to string?
+7.1k Golang : Scanf function weird error in Windows
+7.1k Golang : Example of custom handler for Gorilla's Path usage.
+48.2k Golang : Upload file from web browser to server
+10.5k Golang : Get currencies exchange rates example
+4.7k JQuery : Calling a function inside Jquery(document) block
+6.8k Web : How to see your website from different countries?