Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有自定义css的Golang文件服务器_Go - Fatal编程技术网

具有自定义css的Golang文件服务器

具有自定义css的Golang文件服务器,go,Go,我考虑过用Go为我家创建一个迷你文件服务器。通常http.FileServer服务器文件和目录如下: 是否可以将CSS添加到此站点?例如,改变颜色。提前感谢您的帮助 有一个黑客解决方案,它利用了这样一个事实,即在http.FileServer完成工作后,您可以继续向http.ResponseWriter写入内容。一般不建议使用,但在这种情况下可能可以接受 package main import ( "io" "log" "net/http" )

我考虑过用Go为我家创建一个迷你文件服务器。通常
http.FileServer
服务器文件和目录如下:


是否可以将CSS添加到此站点?例如,改变颜色。提前感谢您的帮助

有一个黑客解决方案,它利用了这样一个事实,即在
http.FileServer
完成工作后,您可以继续向
http.ResponseWriter
写入内容。一般不建议使用,但在这种情况下可能可以接受

package main

import (
        "io"
        "log"
        "net/http"
)

const (
        link = `<link rel="stylesheet" href="/path/to/style.css">`
)

func main() {
        fs := http.FileServer(http.Dir("/tmp"))
        var handler http.HandlerFunc
        handler = func(w http.ResponseWriter, r *http.Request) {
                var (
                        url   = r.URL.Path
                        isDir = url[len(url)-1] == '/'
                )
                fs.ServeHTTP(w, r)
                if isDir {
                        io.WriteString(w, link)
                }
        }
        log.Fatal(http.ListenAndServe(":8080", handler))
}
主程序包
进口(
“io”
“日志”
“net/http”
)
常数(
链接=``
)
func main(){
fs:=http.FileServer(http.Dir(“/tmp”))
变量处理程序http.HandlerFunc
handler=func(w http.ResponseWriter,r*http.Request){
变量(
url=r.url.Path
isDir=url[len(url)-1]=='/'
)
财政司司长(西、右)
中频isDir{
io.WriteString(带链接)
}
}
log.Fatal(http.ListenAndServe(“:8080”,处理程序))
}

不是直接的,但是编写自己的实现以您认为合适的方式列出目录是相对简单的。您可以在标准库中看到实现,并将其用作自己的基础:是的,带有一个链接::check here:非常基本的golang http服务器