What is deadlock in Go?
In Golang, when two processes known as goroutines run concurrently and block each other's pathways, it's known as a deadlock.
Let's look at the following illustration for a better understanding:
In this illustration, we have two processes, Process 1 and Process 2, and there are two resources as variables, Variable i and Variable j. Both Process 1 and Process 2 acquire one of the two variables. At the same time, they both need each other's resources to perform their processes. So they begin to wait for each other to release their respective resource so they could perform their tasks. This mutual anticipation from each other to release the resource becomes a deadlock for both processes.
Let's look at an example of a deadlock in a program.
Example
package mainfunc main() {println("Channels are in Deadlock")Channel := make(chan int)Channel2 := make(chan int)<- Channel<- Channel2}
Explanation
Lines 4–5: We create two channels with the name
ChannelandChannel2.Lines 7–8: Both channels are deadlocked with each other.
Free Resources