Recupere-se em Golang
Assim como o bloco try / catch em exception em linguagens como Java , C# , etc. são usados para capturar exceções da mesma forma na linguagem Go, a função de recuperação é usada para lidar com o pânico. É uma função embutida que é definida no pacote embutido da linguagem Go. O principal uso desta função é recuperar o controle de um Goroutine em pânico. Ou em outras palavras, ele lida com o comportamento de pânico do Goroutine .
Sintaxe:
func recover() interface{}
Pontos importantes:
- A função de recuperação é sempre chamada dentro da função adiada, não na função normal. Se você chamar a função de recuperação dentro da função normal ou fora da função adiada, a função de recuperação não interromperá a sequência de pânico como mostrado no Exemplo 1. Portanto, sempre chamada de função de recuperação dentro da função adiada porque a função adiada não interrompe sua execução se o programa entrar em pânico, a função de recuperação para a sequência de pânico simplesmente restaurando a execução normal da goroutine e recupera o valor de erro passado para a chamada de pânico, conforme mostrado no Exemplo 2.
- A função de recuperação só funciona se você chamar a mesma goroutine em que ocorre o pânico. Se você chamá-lo em uma goroutine diferente, não funcionará como mostrado no Exemplo 3.
- Se você quiser encontrar o rastreamento da pilha, use a função PrintStack que é definida em Debug package.
Exemplo 1:
// Go program which illustrates
// the concept of recover
package main
import "fmt"
// This function is created to handle
// the panic occurs in entry function
// but it does not handle the panic
// occurred in the entry function
// because it called in the normal
// function
func handlepanic() {
if a := recover(); a != nil {
fmt.Println("RECOVER", a)
}
}
// Function
func entry(lang *string, aname *string) {
// Normal function
handlepanic()
// When the value of lang
// is nil it will panic
if lang == nil {
panic("Error: Language cannot be nil")
}
// When the value of aname
// is nil it will panic
if aname == nil {
panic("Error: Author name cannot be nil")
}
fmt.Printf("Author Language: %s \n Author Name: %s\n", *lang, *aname)
fmt.Printf("Return successfully from the entry function")
}
// Main function
func main() {
A_lang := "GO Language"
entry(&A_lang, nil)
fmt.Printf("Return successfully from the main function")
}
Saída:
panic: Error: Author name cannot be nil goroutine 1 [running]: main.entry(0x41a788, 0x0) /tmp/sandbox777592252/prog.go:35 +0x180 main.main() /tmp/sandbox777592252/prog.go:46 +0x40
Exemplo 2:
// Go program which illustrates
// the concept of recover
package main
import (
"fmt"
)
// This function handles the panic
// occur in entry function
// with the help of the recover function
func handlepanic() {
if a := recover(); a != nil {
fmt.Println("RECOVER", a)
}
}
// Function
func entry(lang *string, aname *string) {
// Deferred function
defer handlepanic()
// When the value of lang
// is nil it will panic
if lang == nil {
panic("Error: Language cannot be nil")
}
// When the value of aname
// is nil it will panic
if aname == nil {
panic("Error: Author name cannot be nil")
}
fmt.Printf("Author Language: %s \n Author Name: %s\n", *lang, *aname)
fmt.Printf("Return successfully from the entry function")
}
// Main function
func main() {
A_lang := "GO Language"
entry(&A_lang, nil)
fmt.Printf("Return successfully from the main function")
}
Saída:
RECOVER Error: Author name cannot be nil Return successfully from the main function
Exemplo 3:
// Go program which illustrates
// recover in a goroutine
package main
import (
"fmt"
"time"
)
// For recovery
func handlepanic() {
if a := recover(); a != nil {
fmt.Println("RECOVER", a)
}
}
/* Here, this panic is not
handled by the recover
function because of the
recover function is not
called in the same
goroutine in which the
panic occurs */
// Function 1
func myfun1() {
defer handlepanic()
fmt.Println("Welcome to Function 1")
go myfun2()
time.Sleep(10 * time.Second)
}
// Function 2
func myfun2() {
fmt.Println("Welcome to Function 2")
panic("Panicked!!")
}
// Main function
func main() {
myfun1()
fmt.Println("Return successfully from the main function")
}
Saída:
Welcome to Function 1 Welcome to Function 2 panic: Panicked!! goroutine 6 [running]: main.myfun2() /tmp/sandbox157568972/prog.go:31 +0xa0 created by main.myfun1 /tmp/sandbox157568972/prog.go:24 +0xc0
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva