- Trending Categories
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
Goroutines – Concurrency in Golang
Golang, also known as Go, is a programming language that was designed to be efficient and scalable for modern computing needs. One of its most powerful features is the ability to handle concurrency through the use of Goroutines. In this article, we will discuss what Goroutines are and how they can be used for concurrent programming in Golang.
What are Goroutines?
A Goroutine is a lightweight thread of execution in Golang that allows a program to run multiple tasks simultaneously. Goroutines are managed by the Go runtime, which schedules their execution on available processors. Unlike traditional threads, Goroutines are cheaper to create and consume less memory, making them more efficient for concurrent programming.
Concurrency in Golang with Goroutines
To demonstrate the use of Goroutines for concurrent programming, let's consider a simple program that calculates the sum of a series of numbers. We can write this program using a single Goroutine or multiple Goroutines, depending on the level of concurrency we want to achieve.
Example
Here's a program that calculates the sum of a series of numbers using a single Goroutine −
package main import ( "fmt" ) func main() { sum := 0 for i := 1; i <= 100000; i++ { sum += i } fmt.Println("Sum:", sum) }
Output
Sum: 5000050000
This program uses a single Goroutine to calculate the sum of the numbers from 1 to 100,000. The program works by iterating through each number in the series and adding it to a running total. Once the iteration is complete, the program prints the final sum.
Example
Now let's rewrite the program to use multiple Goroutines for concurrent processing −
package main import ( "fmt" ) func sum(start, end int, result chan<- int) { sum := 0 for i := start; i <= end; i++ { sum += i } result <- sum } func main() { result := make(chan int) go sum(1, 50000, result) go sum(50001, 100000, result) sum1, sum2 := <-result, <-result fmt.Println("Sum:", sum1+sum2) }
Output
Sum: 5000050000
This program uses two Goroutines to calculate the sum of the numbers from 1 to 50,000 and 50,001 to 100,000 respectively. Each Goroutine calculates the sum of its assigned range of numbers and sends the result to a shared channel. The main Goroutine then receives the two results from the channel and adds them together to produce the final sum.
Conclusion
Goroutines provide a powerful way to achieve concurrency in Golang. By allowing programs to run multiple tasks simultaneously, Goroutines can greatly improve the performance and efficiency of concurrent programming. In this article, we have discussed what Goroutines are and how they can be used for concurrent programming in Golang. By following the examples provided, you can learn how to use Goroutines to write efficient and scalable concurrent programs in Golang.