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
+7.6k Golang : Example of how to detect which type of script a word belongs to
+5.9k Golang : Compound interest over time example
+6.9k Golang : Takes a plural word and makes it singular
+20.4k Golang : Secure(TLS) connection between server and client
+11.5k SSL : The certificate is not trusted because no issuer chain was provided
+47.5k Golang : Convert int to byte array([]byte)
+17.1k Golang : How to tell if a file is compressed either gzip or zip ?
+4.9k Golang : Check if a word is countable or not
+21k Golang : Get password from console input without echo or masked
+6.1k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+11.2k Golang : Characters limiter example