Go: HTTP server example

If you run the program below and access the URL http://localhost:8080/world, you will be greated by this page:

Web browser localhost:8080

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HelloServer)
    http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}

Further reading

The Writing Web Applications tutorial shows how to extend this small example into a complete wiki.

The tutorial covers how to:

Comments

Be the first to comment!