Let's begin with running multiple functions concurrently.
Take a look at the code in the following block:
import ( "fmt" "time" ) func main() { names := []string{"tarik", "john", "michael", "jessica"} for _, name := range names { time.Sleep(1 * time.Second) fmt.Println(name) }
ages := []int{1, 2, 3, 4, 5} for _, age:= range ages { time.Sleep(1 * time.Second) fmt.Println(age) } }
You can see from the preceding code that there are two different lists; each list has items that have taken at least a second to finish, but for practice purposes, we're not going to have any actual code but just fmt.Println
. We have added time.Sleep
for a second within each iteration. As seen in the preceding code, we first process the names and then the ages. One thing you can notice is that they're not really dependent on each other; they're actually two different works. So, let's go ahead and run this and see what that it like on our console:
If...