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.2k Golang : Use modern ciphers only in secure connection
+5.1k Golang : How to iterate a slice without using for loop?
+5.1k Golang : How to call function inside template with template.FuncMap
+5.4k Golang : Get environment variable
+8.4k Golang : Meaning of omitempty in struct's field tag
+7.9k Golang : Sort and reverse sort a slice of integers
+8k Golang : Get current, epoch time and display by year, month and day
+14.8k Golang : Find smallest number in array
+4.7k Golang : Skip or discard items of non-interest when iterating example
+7.8k Android Studio : Checkbox for user to select options example
+10.5k Golang : How to convert a number to words
+4.6k Golang : Build new URL for named or registered route with Gorilla webtoolkit example