Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Graceful Shutdown of Server | Go Programming Bootcamp lesson | BraveProgrammer
  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 27 of 60

·Go

Graceful Shutdown of Servers

15 minutes

Course progress45%

Graceful Shutdown

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

Handle termination signals to allow inflight requests to finish.

srv := &http.Server{ Addr: ":8080", Handler: mux }

go func() { log.Fatal(srv.ListenAndServe()) }()

quit := make( os.Signal, )
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
log.Println()
ctx, cancel := context.WithTimeout(context.Background(), *time.Second)
 cancel()
 err := srv.Shutdown(ctx); err !=  { log.Fatalf(, err) }
log.Println()
chan
1
"shutting down server…"
5
defer
if
nil
"forced shutdown: %v"
"server stopped"

Info

Always give your server a timeout; otherwise a hung connection can block shutdown indefinitely.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from Go Programming BootcampBrowse activities
Previous lessonLogging with `log` and Structured Logging (`zap`)Continue to lesson 28Reflection with the `reflect` PackageBack to Go Programming Bootcamp curriculum