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
+28.7k Golang : How to get HTTP request header information?
+7.5k Golang : On lambda, anonymous, inline functions and function literals
+8.1k Golang : How to check if a string with spaces in between is numeric?
+50.2k Golang : Check if item is in slice/array
+18k Golang : Check if directory exist and create if does not exist
+7.2k Golang : Reverse text lines or flip line order example
+4.9k Cash Flow : 50 days to pay your credit card debt
+8.1k Golang : Go as a script or running go with shebang/hashbang style
+14k Golang : How to get Unix file descriptor for console and file
+11.9k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+5.9k Mac OSX : Find large files by size
+28.9k Golang : Remove characters from string example