Created
May 16, 2024 18:18
-
-
Save vituchon/71c9106adb8edc6702287f079c85605e to your computer and use it in GitHub Desktop.
golang Http server - can not HOLD a panic inside a gorutine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
haceme el favor... corre este código y averigualo por vos mismopackage main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/panic", func(w http.ResponseWriter, r *http.Request) { | |
panic("¡Esto es un panic!") | |
}) | |
http.HandleFunc("/panic-in-goroutine", func(w http.ResponseWriter, r *http.Request) { | |
go func() { | |
panic("¡Esto es un panic dentro de una goroutine!") | |
}() | |
}) | |
fmt.Println("Servidor escuchando en http://localhost:8000/") | |
err := http.ListenAndServe(":8000", nil) | |
if err != nil { | |
fmt.Println("Error al iniciar el servidor:", err) | |
} | |
} | |
/* | |
EXTRAÑAMENTE cuando ocurre un panic dentro de una gorutina SE CAGA LA EJECUCIÓN DEL PROGRAMA | |
O SEA KAPUT... CHAU SERVER | |
ahora cuando hay un panic sin gorutina, el programa aguanta.. y sigue siendo un servidor que atiende lls peticiones | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment