go语言 web开发静态资源服务器

go语言 web开发静态资源服务器

go语言qingyu2022-02-24 18:42:28944A+A-

1.png

body
{
    background-color:#d0e4fe;
}
h1
{
    color:orange;
    text-align:center;
}
p
{
    font-family:"Times New Roman";
    font-size:20px;
}
package main

import (
   "github.com/gorilla/mux"
   "html/template" //模板包
   "net/http"
)

var (
   //申明模板变量,自动引入模板包
   templates *template.Template
)

func main() {
   templates = template.Must(template.ParseGlob("templates/*.html"))
   r := mux.NewRouter()
   //普通路由
   r.HandleFunc("/index", cssHandler)
   fs := http.FileServer(http.Dir("./static"))
   r.PathPrefix("/static/").Handler(http.StripPrefix("/static", fs))
   http.ListenAndServe(":8080", r)
}

func cssHandler(w http.ResponseWriter, r *http.Request) {
   w.WriteHeader(http.StatusOK)
   templates.ExecuteTemplate(w, "css.html", nil)
}
点击这里复制本文地址 欢迎来到大黄鸡源码分享网
qrcode

大黄鸡源码编程网 © All Rights Reserved.  
网站备案号:闽ICP备18012015号-4
Powered by Z-BlogPHP
联系我们| 关于我们| 广告联系| 网站管理