Skip to content

Learning Go – Miniblog #10 – HTTP Servers

January 22, 2013

This carries on from here, and started here.

OK, time to look at how to write the HTTP server piece of the project. A quick trawl through the package documentation reveals that the library does indeed provide an HTTP server implementation, but as usual the documentation is extremely terse, and the few examples are nor all that good. Better is this article in the documentation tree. Adapting the code in the article, I came up with this:

package main

import (

func csvHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "CSV stuff [%s]", r.URL.Path[5:])
func main() {
    http.HandleFunc("/csv/", csvHandler)
    http.ListenAndServe(":8080", nil)

This uses the default HTTP server implementation (called http), and says that any URLs that begin with /csv/ should be handled by the csvHandler function. For the moment, I just print out the remainder of the URL path after the /csv/ string. It then starts listening for requests on port 8080.

I now need to decide what kind of URLs I’m going to support. A bit of thought came up with this scheme; this specific URL /csv/index will display an index of the CSV files that I’m going to serve, where the names of the files will be hyperlinks using  the second type of URL, which will look like  /csv/data/filename.csv.  OK, lets see if I can get that working…


From → golang

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: