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
+5k Python : Find out the variable type and determine the type with simple test
+17k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+9.8k Golang : Qt get screen resolution and display on center example
+7.5k Golang : How to detect if a sentence ends with a punctuation?
+6.2k PHP : Get client IP address
+5.8k Linux : Disable and enable IPv4 forwarding
+14k Golang : How to check if a file is hidden?
+6.6k Golang : Embedded or data bundling example
+10k Golang : Translate language with language package example
+11.6k SSL : The certificate is not trusted because no issuer chain was provided
+10.2k Golang : How to get quoted string into another string?
+5.9k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error