golang搭建一个简单的web服务器

 1 package main
 2 
 3 import (
 4     "io/ioutil"
 5     "log"
 6     "net/http"
 7 )
 8 
 9 func main() {
10     http.HandleFunc("/", ShowIndex)
11     log.Fatal(http.ListenAndServe("localhost:8000", nil))
12 }
13 
14 func ShowIndex(w http.ResponseWriter, r *http.Request) {
15     content, _ := ioutil.ReadFile("./index.html")
16     w.Write(content)
17 }
18 //在浏览器上输入 localhost:8000 就可以显示 index.html

 

posted @ 2020-02-16 12:38  鸡儿er  阅读(283)  评论(0编辑  收藏  举报