This is example how wait group works. Main objective of wait group is control concurrency.
In vanilla.go
, we have ordinary program without concurrency. The goal of each program is creating numbers of random integer,
count total numbers of each numbers and print it with its ratios respective to total numbers created.
When we apply go
goroutine, Go does not wait for the process to complete to proceed (non-blocking). While it is a good thing
for execution speed, non-blocking process can be messed up as in no_waitgroup
. Here come the wait group. Wait group have three methods
which is .Add()
, .Done()
, Wait()
. The first we use .Add()
with input of how many process must be done before waiting is over.
.Done()
is a way telling the wait group that a process has been done. .Wait()
halt the process unless all process done which indicated
by number of .Done()
executed is equal to .Add()
input.