golang 1.16 版提供了资源内嵌

import _ "embed"

//go:embed hello.txt
var s string
print(s)


import _ "embed"

//go:embed hello.txt
var b []byte
print(string(b))


import "embed"

//go:embed hello.txt
var f embed.FS
data, _ := f.ReadFile("hello.txt")
print(string(data))


package server

import "embed"

// content holds our static web server content.
//go:embed image/* template/*
//go:embed html/index.html
var content embed.FS

func main() {
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(content))))
    http.ListenAndServe(":8080", nil)
}

参考资料:
https://golang.org/pkg/embed/

标签: none

添加新评论