English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang tutorial di base

Golang istruzioni di controllo

Golang funzioni e metodi

Golang strutture

Golang tagli e array

Golang stringhe (String)

Golang puntatori

Golang interfacce

Golang concorrenza

Golang eccezioni (Error)

Golang altri argomenti

Go Select statement and deadlock deadlock

In the Go language, the select statement is likeSwitch statementIn the select statement, the case statements refer to communication, that is, the send or receive operations on the channel.

Syntax:

select{
    case SendOrReceive1: // Statement
    case SendOrReceive2: // Statement
    case SendOrReceive3: // Statement
    ......
    default: // Statement
 }

In this article, we will learn how to use the default case to avoid deadlock. But first, we need to understand what deadlock is?

Deadlock:When you try to read or write data from a channel but the channel has no value. Therefore, it blocks the current execution of the goroutine and passes control to other goroutines, but if there are no other goroutines available or other goroutines are sleeping due to this situation, the program will crash. This phenomenon is called deadlock. As shown in the following example:

package main
func main() {
    //Create channel
    //Deadlock occurs because there is no goroutine writing
    //Therefore, the select statement is blocked forever
    c := make(chan int)
    select {
    case <-c:
    }
}

Output:

fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()

To avoid this situation, we use the default case in the select statement. In other words, when a deadlock occurs in the program, the default case of the select statement is executed to avoid deadlock. As shown in the following example, we use the default case in the select statement to avoid deadlock.

package main 
  
import "fmt"
  
func main() { 
  
    //Create channel
    c := make(chan int) 
    select { 
    case <-c: 
    default: 
        fmt.Println("!.. Default case..!") 
    } 
}

Output:

!.. Default case..!

When the select statement only has a nil channel, it also allows the use of the default case. As shown in the following example, the channel c is nil, so it executes in the default case. If the default case here is not available, the program will be blocked forever, and a deadlock will occur.

package main
import "fmt"
func main() {
    //Create channel
    var c chan int
    select {
    case x1 := <-c:
        fmt.Println("Value: ", x1)
    default:
        fmt.Println("Default case..!")
    }
}

Output:

Caso di default...!