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.1k Golang : Get FX sentiment from website example
+17.2k Golang : Get path name to current directory or folder
+6.4k Golang : Handling Yes No Quit query input
+12.8k Golang : Get uploaded file name or access uploaded files
+9.5k Golang : Replace a parameter's value inside a configuration file example
+7.5k Linux/Unix : fatal: the Postfix mail system is already running
+15k Golang : How to extract links from web page ?
+8.2k Golang : Read file with ioutil
+2.9k Golang : PGX CopyFrom to insert rows into Postgres database
+19.2k PHP : Convert(cast) int to double/float
+10.1k Golang : Post data with url.Values{}
+13.9k Golang : How to login and logout with JWT example