Static web server using net/http package

net/http package

It has two major parts

1. ServeMux: Its a HTTP request router.
2. Handler: It writes HTTP response headers and bodies.

Project structure

static-web-server
--pages
----about.html
----home.html
--server.go

Source code

server.go

package main

import "net/http"

func main() {
	// Create a ServerMux object
	m := http.NewServeMux()

	// Create a handler
	f := http.FileServer(http.Dir("pages"))

	// Register HTTP URL with handler
	m.Handle("/", f)

	// Run HTTP server listening at port :8080
	http.ListenAndServe(":8080", m)
}

Start server

$ go run server.go


Leave a Reply

Your email address will not be published. Required fields are marked *