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
+14.8k Golang : Get timezone offset from date or timestamp
+8k Golang : Ackermann function example
+19k Golang : Example for DSA(Digital Signature Algorithm) package functions
+8.6k Golang : Get SPF and DMARC from email headers to fight spam
+7.1k Android Studio : AlertDialog to get user attention example
+18k Golang : Find IP address from string
+6.7k Golang : Squaring elements in array
+18.8k Golang : Check if directory exist and create if does not exist
+8.8k Golang : How to check if a string with spaces in between is numeric?
+12.4k Golang : Convert int(year) to time.Time type
+17.2k Golang : Qt image viewer example
+17.9k Golang : Get download file size