Closed
Description
What version of Go are you using (go version
)?
$ go version go1.16.5
Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/vajahatk/Library/Caches/go-build" GOENV="/Users/vajahatk/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/vajahatk/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/vajahatk/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GOVCS="" GOVERSION="go1.16.5" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/Users/vajahatk/work/dotpe/location/go.mod" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dv/g91vgh2x58b0b8pcxv8qr_yh0000gn/T/go-build4233566118=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
startServer()
}
func startServer() {
mainchan := make(chan struct{})
go func(ch chan<- struct{}) {
defer func() {
if r := recover(); r != nil {
fmt.Println("recovered from crash\n", r)
ch <- struct{}{}
}
}()
http.HandleFunc("/divide", func(w http.ResponseWriter, req *http.Request) {
go panic("this is to be catched")
fmt.Fprintf(w, "Hello, \nyour question is submitted\t")
})
log.Fatal(http.ListenAndServe(":8081", nil))
}(mainchan)
<-mainchan
fmt.Println("safe landing")
}
What did you expect to see?
safe landing
What did you see instead?
panic: this is to be catched
goroutine 7 [running]:
panic(0x1242be0, 0xc000012060)
/usr/local/go/src/runtime/panic.go:1065 +0x565
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1371 +0x1
created by main.startServer.func1.2
/Users/vajahatk/work/test/main.go:29 +0x1e5
exit status 2