完成项目后的目录结构:
完成项目后的源码:
package main
import (
"fmt"
"html/template"
"net/http"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.ParseFiles("views/index.html"))
t.Execute(w, "")
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("views/static"))))
http.Handle("/pages/", http.StripPrefix("/pages/", http.FileServer(http.Dir("views/pages"))))
http.HandleFunc("/main", IndexHandler)
fmt.Println("在监听:http://localhost:8080")
err := http.ListenAndServe(":8080", nil)