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
+4.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+18.3k Golang : Iterating Elements Over A List
+4.7k Golang : Calculate a pip value and distance to target profit example
+14.7k Golang : package is not in GOROOT during compilation
+47.6k Golang : How to convert JSON string to map and slice
+11.7k Golang : Find and draw contours with OpenCV example
+14.9k Golang : Get HTTP protocol version example
+23k Golang : Get ASCII code from a key press(cross-platform) example
+6.5k Golang : Humanize and Titleize functions
+7.2k Golang : Check to see if *File is a file or directory
+8k Golang : Qt splash screen with delay example
+9.2k Golang : Get all countries currencies code in JSON format