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
+17.7k Android Studio : AlertDialog and EditText to get user string input example
+4.4k Fix yum-complete-transaction error
+15.5k Golang : Qt image viewer example
+8.5k Android Studio : Simple input textbox and intercept key example
+10k Golang : Concurrency and goroutine example
+21.2k Golang : Gorilla mux routing example
+7.5k Golang : Handle sub domain with Gin
+17.6k Golang : Execute shell command
+3.6k Golang : A program that contain another program and executes it during run-time
+6.5k Golang : Scan files for certain pattern and rename part of the files
+17.7k Swift : Convert (cast) Int to int32 or Uint32
+4.5k Cash Flow : 50 days to pay your credit card debt