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 chapter11/recipe08.
  2. Navigate to the directory.
  3. Create the file handlegroups.go with the following content:
        package main        import (          "fmt"          "log"          "net/http"        )         func main() {           log.Println("Staring server...")           // Adding to mani Mux           mainMux := http.NewServeMux()           mainMux.Handle("/api/",           http.StripPrefix("/api", restModule()))           mainMux.Handle("/ui/",           http.StripPrefix("/ui", uiModule()))           if err := http.ListenAndServe(":8080", mainMux); err != nil {             panic(err)           }         }         func restModule() http.Handler {           // Separate Mux for all REST           restApi := http.NewServeMux()           restApi.HandleFunc("/users", func(w http.ResponseWriter,                              r *http.Request) {             w.Header().Set("Content-Type", "application/json") fmt.Fprint(w, `[{"id":1,"name":"John"}]`) ...

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