Go Simple HTTP Server Demo

Build an http server in go is really easy. By using net/http, we can build a http server easily.

Simple HTTP Server#

main.goview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import (
"net/http"
)

func hello(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello, world!"))
}

func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":8000", nil)
}

Run go run main.go

Visit http://127.0.0.1:8000/

Hello, world!

The net/http package provided by golang is really powerful, so sometimes you even don’t need any web framework for a small project.

Server Mux#

mux.goview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import (
"net/http"
)

func hello(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello, world!"))
}

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
http.ListenAndServe(":8000", mux)
}

Run go run mux.go

We use a new mux as http handler.

Why Should We Use Mux#

If we don’t use mux, http.HandleFunc makes http package messy. It polluted the global namespace. It’s not so good when we are developing a package. We shouldn’t modify other packages. We can only do things with a new instance.

The first example use nil handler. When nil provided, http.DefaultServeMux is used. All routes are registered in default mux. Any other package may be affected by this code.

When we use a new mux, routes are save in this mux. We can create multiple server listening different ports. Run them with goroutines.

1
2
3
go http.ListenAndServe(":8000", mux)
go http.ListenAndServe(":8001", mux1)
// wait