1. 程式人生 > >Golang搭建靜態伺服器- 兩行程式碼搞定

Golang搭建靜態伺服器- 兩行程式碼搞定

windows下,新建資料夾D:\webserver\gowww\site

並放入一個靜態網站的所有檔案


新建go原始檔 staticweb.go

// staticweb
package main

import (
	"net/http"
)

func main() {
	http.Handle("/", http.FileServer(http.Dir("D:/webserver/gowww/site/")))
	http.ListenAndServe(":9090", nil)
}

F5執行後, 瀏覽器訪問http://localhost:9090


只需兩行程式碼

go來搭建靜態web伺服器,就是這麼簡單,這麼有效。