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
+5.7k Golang : Muxing with Martini example
+16.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+4.2k Python : Convert(cast) string to bytes example
+5.3k Golang : Map within a map example
+13.1k Golang : Overwrite previous output with count down timer
+8.2k Golang : How to get garbage collection data?
+39.2k Golang : How to count duplicate items in slice/array?
+17k Golang : Find IP address from string
+9k Golang : Random Rune generator
+21.4k Golang : Test file read write permission example
+9.1k Golang : Embed secret text string into binary(executable) file
+6.4k Javascript : Push notifications to browser with Push.js