| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 
 | func main() {group, ctx := errgroup.WithContext(context.Background())
 
 mux := http.NewServeMux()
 mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
 fmt.Fprintln(w, "Hello Go")
 })
 
 server := http.Server{
 Handler: mux,
 Addr:    ":8889",
 }
 
 
 serverOut := make(chan struct{})
 mux.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
 serverOut <- struct{}{}
 })
 
 
 
 
 
 group.Go(func() error {
 return server.ListenAndServe()
 })
 
 
 
 group.Go(func() error {
 select {
 case <-serverOut:
 fmt.Println("server closed")
 case <-ctx.Done():
 fmt.Println("errgroup exit")
 }
 
 timeoutCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
 defer cancel()
 log.Println("shutting down server...")
 return server.Shutdown(timeoutCtx)
 })
 
 
 
 
 
 group.Go(func() error {
 quit := make(chan os.Signal, 1)
 
 signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
 
 select {
 case <-ctx.Done():
 return ctx.Err()
 case sig := <-quit:
 return errors.Errorf("get os exit: %v", sig)
 }
 })
 
 
 err := group.Wait()
 fmt.Println(err)
 fmt.Println(ctx.Err())
 }
 
 |