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
+45.1k Golang : Encode image to base64 example
+20.4k Golang : Convert string slice to struct and access with reflect example
+36.6k Golang : Read a text file and replace certain words
+8.1k Golang : Take screen shot of browser with JQuery example
+8.2k Golang : Write multiple lines or divide string into multiple lines
+6.1k Golang : Pat multiplexer routing example
+28.2k Golang : Save map/struct to JSON or XML file
+10.6k Get form post value in Go
+4.6k Unix/Linux/MacOSx : How to remove an environment variable ?
+24.8k Golang : missing Mercurial command
+5.4k Golang : Process non-XML/JSON formatted ASCII text file example
+14.3k Golang : How do I get the local IP (non-loopback) address ?