A normal example is this:
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
func() {
time.Sleep(1)
ch <- 1
}()
fmt.Println(<-ch)
}
The output should be:
# go run test.go
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main.func1(...)
/root/test.go:17
main.main()
/root/test.go:18 +0x47
exit status 2
But when I add the code to connect to the database like this:
package main
import (
"fmt"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
db, err := gorm.Open(mysql.Open("dsn"))
fmt.Printf("%v, %s\n", db, err)
ch := make(chan int)
func() {
time.Sleep(1)
ch <- 1
}()
fmt.Println(<-ch)
}
It doesn't report an error, it keeps blocking, the output is(I ended it with ctrl+c):
# go run test.go
&{0xc00015c510 <nil> 0 0xc00016c380 1}, %!s(<nil>)
^Csignal: interrupt
Can anyone help me, thanks.