My favorites | Sign in
Project Home Wiki
Search
for
HttpStaticFiles  
Serving static files with HTTP.
http
Updated Mar 30, 2012 by minux...@gmail.com

Serving Static Files with HTTP

The HTTP package provides good support for efficiently serving static files.

This is a complete Go webserver serving static files:

package main
import "net/http"
func main() {
	panic(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
}

That example is intentionally short to make a point. Using panic() to deal with an error is probably too aggressive & would produce too much output. More typical style would be:

package main

import (
	"net/http"
	"log"
)

func main() {
	err := http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc")))
	if err != nil {
		log.Printf("error running docs webserver: %v", err)
	}
}

For details, see: http://golang.org/pkg/net/http/#FileServer

Powered by Google Project Hosting