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
+13.8k Golang : How to check if your program is running in a terminal
+20.6k Golang : Sort and reverse sort a slice of strings
+11.4k Golang : Convert decimal number(integer) to IPv4 address
+13.5k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+8.7k Golang : does not implement flag.Value (missing Set method)
+26.6k PHP : Convert(cast) string to bigInt
+7.3k Golang : Test if an input is an Armstrong number example
+7.7k Golang : Metaprogramming example of wrapping a function
+18.1k Unmarshal/Load CSV record into struct in Go
+24.6k Golang : Convert long hexadecimal with strconv.ParseUint example
+11.2k How to tell if a binary(executable) file or web application is built with Golang?
+21.9k Golang : Convert Unix timestamp to UTC timestamp