- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to print the numbers 1 to 100 concurrently, with each number printed by a separate goroutine
In this article, we will write a Go language program to print the numbers from 1 to 100 concurrently with each number printed by a separate go routine. Concurrency means execution of multiple tasks parallelly and independently and making sure all the resources of the system are utilized. It can be achieved via go routines which are light weight threads and the channels.
Syntax
func make ([] type, size, capacity)
The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.
Algorithm
This program imports fmt, main and sync as necessary packages.
Create a function named print_number with two parameters the number n and a pointer to the sync.Wait group.
Inside this function, print the number using the Println function and using the Done method notify when the printing is don.
Create a main function.
In the main, initialize the sync.Wait Group(wg).
In this step, iterate from 1 to 100 using a for loop and in every iteration add a go routine to sync.Wait Group using the Add method.
For each number increment the sync.Wait Group counter by calling wg.Add(1).
Launch go routine to print the number by calling go print_number(i, &wg).
In this step, wait for all go routines to finish using the wg.Wait() method This blocks the program's execution until the sync.Wait Group counter reaches zero.
After all go routines have finished the program terminates.
Example 1
In this article, we will write a Go language program to print the numbers concurrently from 1 to 100 using sync.Wait group to show the completion of Go routine.
package main import ( "fmt" "sync" ) func print_number(n int, wg *sync.WaitGroup) { defer wg.Done() fmt.Println(n) } func main() { var wg sync.WaitGroup fmt.Println("The numbers from 1 to 100 printed concurrently are:") for i := 1; i <= 100; i++ { wg.Add(1) go print_number(i, &wg) } wg.Wait() }
Output
The numbers from 1 to 100 printed concurrently are: 100 51 52 53 76 26 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 64 54 55 56 57 58 59 60 61 62 63 12 1 2 3 4 5 6 7 8 9 10 11 70 65 66 67 68 69 37 77 71 72 73 74 75 31 27 28 29 30 43 38 39 40 41 42 34 32 33 35 18 13 14 15 16 17 36 46 44 45 21 19 20 23 22 24 48 47 49 50 25
Example 2
In this article, we will write a Go language program to print the numbers concurrently from 1 to 100 using the done to show the completion of Go routine.
package main import ( "fmt" ) func print_number(n int, done chan bool) { fmt.Println(n) done <- true } func main() { done := make(chan bool) fmt.Println("The numbers printed concurrently from 1 to 100 are:") for i := 1; i <= 100; i++ { go print_number(i, done) } for i := 1; i <= 100; i++ { <-done } }
Output
The numbers printed concurrently from 1 to 100 are: 100 51 77 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 89 78 79 80 81 82 83 84 85 86 87 88 57 52 53 54 55 56 69 64 65 66 67 68 72 70 71 95 90 91 92 93 94 74 73 60 58 59 98 96 97 61 62 50 99 1 75 76 63
Conclusion
We compiled and executed the program of printing the numbers concurrently from 1 to 100 using two examples. In the first example, we used sync.Wait group and in the second example we used done to denote completion of go routine.