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
+18.4k Golang : Count number of digits from given integer value
+6k Golang : Rename part of filename
+14.5k Golang : Test floating point numbers not-a-number and infinite example
+4.8k Golang : Experimenting with the Rejang script
+34.2k Golang : Display float in 2 decimal points and rounding up or down
+23.1k Golang : Get current file path of a file or executable
+13.5k Golang : Get timezone offset from date or timestamp
+5.9k Golang : Calculate how many weeks left to go in a given year
+8k Golang : Qt get screen resolution and display on center example
+6.7k Golang : Find relative luminance or color brightness
+30.6k Golang : Regular Expression for alphanumeric and underscore
+18k Golang : How to Set or Add Header http.ResponseWriter?