Press "Enter" to skip to content

用Go写一个简单的web服务器

webserver.go

package main

import(
"net/http"
)
 
func tList(w http.ResponseWriter, r *http.Request){
	w.Header().Set("Content-Type", "application/json;charset=utf-8")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Write([]byte(`{"data":{"tlist":[{"id":1, "name":"sunjingming"},{"id":2, "name":"liulll"}]}}`))
}
func main() {
	http.HandleFunc("/", tList)
	http.ListenAndServe(":8000", nil)
}

执行: go run webserver.go

发表评论