macos\u7a0b\u5e8f\u5f00\u53d1<\/a><\/p>\n\u2502 \u251c\u2500\u2500 css<\/p>\n
\u2502 \u2502 \u2514\u2500\u2500 style.css<\/p>\n
\u2502 \u2514\u2500\u2500 js<\/p>\n
\u2502 \u2514\u2500\u2500 app.js<\/p>\n
“`<\/p>\n
\u6211\u4eec\u53ef\u4ee5\u5728`main.go`\u6587\u4ef6\u4e2d\u5f15\u5165`packr`\u5305\u5e76\u521b\u5efa\u4e00\u4e2a`packr.Box`\u5bf9\u8c61\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
“`go<\/p>\n
package main<\/p>\n
import (<\/p>\n
“net\/http”<\/p>\n
“github.com\/gobuffalo\/packr”<\/p>\n
)<\/p>\n
func main() {<\/p>\n
\/\/ \u521b\u5efa\u4e00\u4e2apackr.Box\u5bf9\u8c61\uff0c\u7528\u4e8e\u6253\u5305\u9759\u6001\u8d44\u6e90\u6587\u4ef6<\/p>\n
staticBox := packr.NewBox(“.\/static”)<\/p>\n
templateBox := packr.NewBox(“.\/templates”)<\/p>\n
\/\/ \u6ce8\u518c\u9759\u6001\u8d44\u6e90\u6587\u4ef6\u5904\u7406\u5668<\/p>\n
http.Handle(“\/static\/”, http.StripPrefix(“\/static\/”, http.FileServer(staticBox)))<\/p>\n
\/\/ \u6ce8\u518c\u6a21\u677f\u5904\u7406\u5668<\/p>\n
http.HandleFunc(“\/”, func(w http.ResponseWriter, r *http.Request) {<\/p>\n
tmpl, err := templateBox.FindString(“home.html”)<\/p>\n
if err != nil {<\/p>\n
http.Error(w, err.Error(), http.StatusInternalServerError)<\/p>\n
return<\/p>\n
}<\/p>\n
w.Write([]byte(tmpl))<\/p>\n
})<\/p>\n
\/\/ \u542f\u52a8Web\u670d\u52a1\u5668<\/p>\n
http.ListenAndServe(“:8080”, nil)<\/p>\n
}<\/p>\n
“`<\/p>\n
\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a`packr.Box`\u5bf9\u8c61\uff0c\u5206\u522b\u7528\u4e8e\u6253\u5305\u9759\u6001\u8d44\u6e90\u6587\u4ef6\u548c\u6a21\u677f\u6587\u4ef6\u3002\u7136\u540e\uff0c\u6211\u4eec\u6ce8\u518c\u4e86\u4e00\u4e2a\u9759\u6001\u8d44\u6e90\u6587\u4ef6\u5904\u7406\u5668\u548c\u4e00\u4e2a\u6a21\u677f\u5904\u7406\u5668\u3002\u5728\u6a21\u677f\u5904\u7406\u5668\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528`templateBox.FindString`\u65b9\u6cd5\u83b7\u53d6\u6a21\u677f\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n
3. \u4f7f\u7528packr\u6253\u5305Web\u5e94\u7528\u7a0b\u5e8f<\/p>\n
\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u7528`packr`\u5de5\u5177\u5c06Web\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1a<\/p>\n
“`<\/p>\n
packr bui<\/p>\n
<\/figure>\n<\/p>\nld<\/p>\n
“`<\/p>\n
\u8be5\u547d\u4ee4\u4f1a\u5c06Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u9759\u6001\u8d44\u6e90\u6587\u4ef6\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5e76\u5c06Web\u5e94\u7528\u7a0b\u5e8f\u7684\u53ef\u6267\u884c\u6587\u4ef6\u548c\u8be5\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e00\u8d77\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002<\/p>\n
4. \u90e8\u7f72Web\u5e94\u7528\u7a0b\u5e8f<\/p>\n
\u5c06\u6253\u5305\u540e\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u548c\u5176\u4ed6\u76f8\u5173\u6587\u4ef6\u4e00\u8d77\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u4e2d\u5373\u53ef\u3002<\/p>\n
## \u603b\u7ed3<\/p>\n
\u4f7f\u7528`packr`\u5de5\u5177\u53ef\u4ee5\u5c06Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u9759\u6001\u8d44\u6e90\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u4ece\u800c\u65b9\u4fbf\u90e8\u7f72\u548c\u7ba1\u7406\u3002\u4f7f\u7528`packr`\u6253\u5305Web\u5e94\u7528\u7a0b\u5e8f\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u5b89\u88c5`packr`\u5de5\u5177\u3001\u5728Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5f15\u5165`packr`\u5305\u3001\u4f7f\u7528`packr`\u6253\u5305Web\u5e94\u7528\u7a0b\u5e8f\u3001\u90e8\u7f72Web\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
Go\u8bed\u8a00\u662f\u4e00\u79cd\u9002\u5408\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u7684\u9ad8\u6548\u7f16\u7a0b\u8bed\u8a00\u3002Go\u8bed\u8a00\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7528\u4e8e\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u7684\u5305\uff0c\u5982`net\/http`\u5305\u548c`html\/template`\u5305\u3002\u5f53\u6211\u4eec\u5f00\u53d1\u5b8c\u6211\u4eec\u7684Web\u5e94\u7528\u7a0b\u5e8f<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[332,4934,18,116,4935],"topic":[],"class_list":["post-2348","post","type-post","status-publish","format-standard","hentry","category-zhuomianruanjian","tag-332","tag-exe","tag-18","tag-116","tag-4935"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/2348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=2348"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/2348\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=2348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=2348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=2348"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=2348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}