Creating the HTTP Server
The creation of the HTTP server in Go is very easy, and the standard library provides more ways of how to do that. Let's look at the very basic one.
How to do it...
- Open the console and create the folder
chapter09/recipe04
. - Navigate to the directory.
- Create the
httpserver.go
file with the following content:
package main import ( "fmt" "net/http" ) type SimpleHTTP struct{} func (s SimpleHTTP) ServeHTTP(rw http.ResponseWriter, r *http.Request) { fmt.Fprintln(rw, "Hello world") } func main() { fmt.Println("Starting HTTP server on port 8080") // Eventually you can use // http.ListenAndServe(":8080", SimpleHTTP{}) s := &http.Server{Addr: ":8080", Handler: SimpleHTTP{}} s.ListenAndServe() }
- Execute the code by
go run httpserver.go
. - See the output:

- Access the URL
http://localhost:8080
in a browser or...