This section will present a way to prevent the main() function from ending while it is waiting for its goroutines to finish using the sync package. The logic of the syncGo.go program will be based on create.go, which was presented in the previous section.
The first part of syncGo.go is as follows:
package main import ( "flag" "fmt" "sync" )
As you can see, there is no need to import and use the time package, as we will use the functionality of the sync package and wait for as long as necessary for all the goroutines to end.
In Chapter 10, Concurrency in Go – Advanced Topics, you will study two techniques for timing out goroutines when they are taking longer than desired.
The second code segment of syncGo.go is shown in the following Go code:
func main() { n := flag.Int("n...