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 : Join lines with certain suffix symbol example
+12.5k Golang : Pass database connection to function called from another package and HTTP Handler
+10.9k How to test Facebook App on localhost ?
+5.9k Golang : Experimenting with the Rejang script
+37.9k Golang : Read a text file and replace certain words
+6.2k Golang : How to search a list of records or data structures
+8.1k Golang : Auto-generate reply email with text/template package
+16.9k Golang : XML to JSON example
+17.7k Golang : Simple client server example
+29k Golang : missing Git command
+7.7k Golang : Regular Expression find string example