O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter09/recipe11.
  2. Navigate to the directory.
  3. Create the file gracefully.go with the following content:
        package main        import (          "context"          "fmt"          "log"          "net/http"          "os"          "os/signal"          "time"        )        func main() {          mux := http.NewServeMux()          mux.HandleFunc("/",func(w http.ResponseWriter, r *http.Request){            fmt.Fprintln(w, "Hello world!")          })          srv := &http.Server{Addr: ":8080", Handler: mux}          go func() {            if err := srv.ListenAndServe(); err != nil {              log.Printf("Server error: %s\n", err)            }          }()          log.Println("Server listening on : " + srv.Addr)          stopChan := make(chan os.Signal)          signal.Notify(stopChan, os.Interrupt)          <-stopChan // wait for SIGINT          log.Println("Shutting down server...") ctx, cancel := context.WithTimeout( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required