xq 1 vuosi sitten
vanhempi
commit
ffd8e7ab59
1 muutettua tiedostoa jossa 44 lisäystä ja 0 poistoa
  1. 44 0
      README.md

+ 44 - 0
README.md

@@ -41,4 +41,48 @@ GOOS=windows GOARCH=amd64 go build -o webup-windows-amd64.exe -a -ldflags '-w -s
 
 ```
 
+- go语言静态文件嵌入
+
+```
+;例子目录结构
+# tree 
+├── main.go
+└── static
+    ├── fs.go
+    ├── jquery-3.1.1.min.js
+    ├── md5.js
+    └── upload.html
+
+```
+
+```go
+//例子文件 static/fs.go
+package static
+
+import (
+	"embed"
+)
+
+//go:embed *.css *.js *.html
+var FS embed.FS
+
+```
+
+```go
+//例子文件 main.go
+package main
+
+import (
+	"net/http"
+	"webuploader/static"	//本地文件夹 static
+)
+
+func main() {
+	fs := http.FileServer(http.FS(static.FS))
+	http.Handle("/path/", http.StripPrefix("/path/", fs))
+
+	http.ListenAndServe(":8080", nil)
+}
+
+```