Go Web 示例: 资源和文件

资源和文件

示例将展示如何从特定目录提供静态文件,如 CSS、JavaScript 或图像。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// static-files.go
package main

import "net/http"

func main() {
    fs := http.FileServer(http.Dir("assets/"))
    http.Handle("/static/", http.StripPrefix("/static/", fs))

    http.ListenAndServe(":8080", nil)
}
$ tree assets/
assets/
└── css
    └── styles.css
$ go run static-files.go

$ curl -s http://localhost:8080/static/css/styles.css
body {
    background-color: black;
}

本文翻译自:https://gowebexamples.com/static-files/

updatedupdated2021-07-032021-07-03
Load Comments?