0% found this document useful (0 votes)
37 views9 pages

Goroutines - Concurrency in Go 1

The document discusses goroutines in Go for concurrency. It explains how goroutines allow running functions concurrently without blocking other code. It also covers using sync.WaitGroup to synchronize goroutines and wait for them to finish.

Uploaded by

Bahadur Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views9 pages

Goroutines - Concurrency in Go 1

The document discusses goroutines in Go for concurrency. It explains how goroutines allow running functions concurrently without blocking other code. It also covers using sync.WaitGroup to synchronize goroutines and wait for them to finish.

Uploaded by

Bahadur Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Goroutines - Concurrency in Go

To save a string to a variable

var ticket = fmt.Sprintf("%v tickets for %v %v", userTickets, firstName, lastName)

“time” - functionality for time

The sleep function stops or blocks the current “thread” (goroutine) execution for the
defined duration

func sendTicket(userTickets uint, firstName string, lastName string, email string){


time.Sleep(10 * time.Second)
var ticket = fmt.Sprintf("%v tickets for %v %v", userTickets, firstName, lastName)
fmt.Println("####################")
fmt.Printf("Sending ticket:\n%v \nto email address %v\n", ticket, email)
fmt.Println("####################")
}
//This function will be executed after 10 seconds
//time is a package

Now suppose the code in the function in real world takes so much time for execution
that it blocks the other entire code. Handle this blocking code with Goroutines.

Goroutines - Concurrency in Go 1
Goroutines - Concurrency in Go 2
Goroutines - Concurrency in Go 3
For concurrent code, just use the keyword “go”.

go sendTicket(userTickets, firstName, lastName, email)

“go …..” - starts a new goroutine

A goroutine is a lightweight thread managed by the Go runtime

Synchronizing the Goroutines


Suppose in the code logic for the main, we remove the for loop that is now after only
one entry the program will stop.

Goroutines - Concurrency in Go 4
Goroutines - Concurrency in Go 5
To perform this, first we need to create a Waitgroup before main()

Waits for the launched goroutine to finish

Package “sync” provides basic synchronization functionality

var wg = sync.WaitGroup{}

This wg has additional functions:

Add: Sets the number of goroutines to wait for (increases the counter by the
provided number)

Wait: Blocks until the WaitGroup counter is 0

Goroutines - Concurrency in Go 6
Done: Decrements the WaitGroup counter by 1. So this is called by the
goroutine to indicate that it’s finished

Goroutines - Concurrency in Go 7
Goroutines - Concurrency in Go 8
Goroutines - Concurrency in Go 9

You might also like