Golang net/http.NewServeMux() and ServeMux.Handle() functions example

package net/http

Golang net/http.NewServeMux() and ServeMux.Handle() functions usage example

 package main

 import (
  "net/http"
 )

 func SayHelloWorld(w http.ResponseWriter, r *http.Request) {
  html := "Hello"
  html = html + " World"

  w.Write([]byte(html))
 }

 func main() {
  mux := http.NewServeMux()
  mux.Handle("/", SayHelloWorld)

  http.ListenAndServe(":8080", mux)
 }

References :

http://golang.org/pkg/net/http/#NewServeMux

http://golang.org/pkg/net/http/#ServeMux.Handle

https://www.socketloop.com/tutorials/golang-http-server-example

Advertisement