Web Server

We use the http package in Go a lot. so this group of pages is written to explore the possibilities.

Expected outline

  • Simple web server serving the current directories pages. Almost a one liner.
  • Hello world! Show calling go code for a web page contenthttps://pkg.go.dev/net/http#hdr-Patterns.
  • Explore the new patterns for the handlers. Since 1.22 (maybe 1.21?) the patterns are greatly enhanced so we might want to try creating different patterns. Doc at
  • Hello George. (broken) Show handling a form value. And show the problem of echoing user input.
  • Hello George 2. Use http templates to quote user code for safety.
  • TLS. It isn’t as hard to use a certificate as it is to procure one.
  • TLS safer. Remove old cyphers to make the server safer (and get by Infosec).

Someday Maybe

The articles above ignores any code in the browser. If you are writing apps you might want fancier features like websockets, graphql, logins etc. Maybe I could explore these ideas further.

Subsections of Web Server

One Line Web Server

There takes one line to create a web server in a Go program.

	http.ListenAndServe(":8080", http.FileServer(http.Dir(".")))

This will serve files from the current directory and below. It protects against leaving the directory with ../ etc. But it does show dot files. The example in the documentation can be found at example-FileServer

The unexpected part here is that the second parameter to ListenAndServe is a handler. Usually we put a ServeMux there but anything that looks like an http.Handler will work. It is also common to put nil there so the system uses the default handler.