Skip to content

Instantly share code, notes, and snippets.

@erasin
Created August 26, 2013 09:36
Show Gist options
  • Save erasin/6339684 to your computer and use it in GitHub Desktop.
Save erasin/6339684 to your computer and use it in GitHub Desktop.
golang 构建简单的服务器,和静态文件
package main
import (
"io"
"log"
"net/http"
"os"
)
func Hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hellow world!")
}
func main() {
// ServeHTTP fun
http.HandleFunc("/", Hello)
// 静态文件 os 绝对路径
wd, err := os.Getwd() // 当前路径
if err != nil {
log.Fatal(err)
}
// 前缀去除 ;列出dir
http.Handle("/static/",
http.StripPrefix("/static/",
http.FileServer(http.Dir(wd))))
err = http.ListenAndServe(":4000", nil)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment