Hello world HTTP server example

yourbasic.org/golang

A basic web server

If you access the URL http://localhost:8080/world on a machine where the program below is running, you will be greeted 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: a complete wiki

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: