codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

17 / 72 topics
17Concurrency Basics18Goroutines19Channels20Select Statement21Sync Package
Tutorials/Go (Golang)/Concurrency
🐹Go (Golang)

Concurrency

Updated 2026-05-15
10 min read

Concurrency

Introduction

Concurrency is a fundamental concept in modern software development, especially when building scalable and efficient applications. In Go, concurrency is built into the language from the ground up, making it easy to write programs that can handle multiple tasks simultaneously. This tutorial will introduce you to the basics of concurrency in Go, focusing on goroutines and channels.

Concept

Concurrency in Go is primarily achieved through two mechanisms: goroutines and channels.

  • Goroutines: These are lightweight threads managed by the Go runtime. They allow you to run multiple functions concurrently.
  • Channels: These are used for communication between goroutines. Channels provide a way to synchronize data exchange between different parts of your program.

Examples

Goroutines

Let's start with an example that demonstrates how to use goroutines in Go.

Go
1package main
2
3import (
4 "fmt"
5 "time"
6)
7
8func sayHello() {
9 for i := 0; i < 5; i++ {
10 fmt.Println("Hello from goroutine")
11 time.Sleep(1 * time.Second)
12 }
13}
14
15func main() {
16 go sayHello()
17 fmt.Println("Hello from main function")
18
19 // Wait for the goroutine to finish
20 time.Sleep(6 * time.Second)
21}

In this example, we define a sayHello function that prints "Hello from goroutine" five times with a one-second delay between each print. We then start a goroutine by calling go sayHello() in the main function. The main function also prints "Hello from main function".

To ensure that the program waits for the goroutine to finish before exiting, we use time.Sleep(6 * time.Second) at the end of the main function. This is a simple way to synchronize the execution, but in real-world applications, you would typically use channels or other synchronization primitives.

Channels

Channels are used to communicate between goroutines and synchronize their execution. Let's see how channels work with an example.

Go
1package main
2
3import (
4 "fmt"
5)
6
7func worker(id int, ch chan int) {
8 for n := range ch {
9 fmt.Printf("Worker %d received %d
10", id, n)
11 }
12}
13
14func main() {
15 ch := make(chan int)
16
17 // Start three workers
18 for i := 1; i <= 3; i++ {
19 go worker(i, ch)
20 }
21
22 // Send numbers to the channel
23 for i := 0; i < 5; i++ {
24 ch <- i
25 }
26
27 close(ch) // Close the channel when done sending
28
29 // Wait for all workers to finish
30 time.Sleep(1 * time.Second)
31}

In this example, we define a worker function that receives integers from a channel and prints them. In the main function, we create a channel of type int using make(chan int). We then start three worker goroutines, each receiving messages from the same channel.

We send five numbers to the channel using ch <- i, and after sending all numbers, we close the channel using close(ch). This signals to the workers that no more messages will be sent. Finally, we use time.Sleep(1 * time.Second) to wait for the workers to finish processing the messages.

What's Next?

In this tutorial, you learned about the basics of concurrency in Go, including goroutines and channels. In the next section, we will dive deeper into goroutines, exploring how they are scheduled, managed, and used in more complex scenarios.

Stay tuned for more advanced topics on Go concurrency!


PreviousInterfacesNext Goroutines

Recommended Gear

InterfacesGoroutines