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
+16.2k Golang : Get sub string example
+24.9k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+26.1k Golang : Daemonizing a simple web server process example
+14.9k Golang : GUI with Qt and OpenCV to capture image from camera
+27.9k Golang : dial tcp: too many colons in address
+32.7k Golang : Math pow(the power of x^y) example
+9k Golang : Get final balance from bit coin address example
+6.7k Golang : Convert an executable file into []byte example
+5.3k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+6.2k Golang : Experimenting with the Rejang script
+22.4k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+7.9k Golang : Scan files for certain pattern and rename part of the files