Created
August 26, 2013 09:36
-
-
Save erasin/6339684 to your computer and use it in GitHub Desktop.
golang 构建简单的服务器,和静态文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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