HTTP server example

yourbasic.org/golang

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:])
}

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

The tutorial covers how to

More code examples

Go blueprints: code for com­mon tasks is a collection of handy code examples.

Share this page: