Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
done <- struct{}{} в большинстве случаем можно и принято использовать close(done).func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGHUP, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
go func() {
<-signals
fmt.Println("cancel")
cancel()
}()
done := make(chan struct{})
go func() {
for {
select {
case <-ctx.Done():
close(done)
return
}
}
}()
<-done
fmt.Println("exit")
}
func doYourStuff(ctx context.Context) <-chan struct{} {
done := make(chan struct{})
go func() {
for {
select {
case <-ctx.Done():
close(done)
return
}
}
}()
return done
}func () {
v := 0
return &v
}Each time the «defer» statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked.
Запись в «занулённый» канал из ранее запущенной goroutine (завешивает «навечно» функцию со всеми объектами).
ну, чего ты докопался, в самом деле?
pprof в golang: Исправляем утечку памяти