网络知识 科技 Go Gin框架Web应用embed打包静态文件和模板文件

Go Gin框架Web应用embed打包静态文件和模板文件

{"data":{"title":"Go Gin框架Web应用embed打包静态文件和模板文件","abstract":"目录结构main.go主文件代码package mainimport (t"embed"t"github.","cover":"https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/454da09f01f34571b6495d1b51dcbc5e","articleType":"article","itemId":"7112656002809610786","groupId":"7112656002809610786","groupSource":2,"isOriginal":true,"banComment":false,"publishTime":"2022-06-24 12:18","source":"GoPython","tag":"technique","mediaSite":null,"pathname":"/article/7112656002809610786/","loginUserInfo":null,"favorite":false,"relation":{"isFollowing":false,"isFollowed":false},"likeData":{"userLikeStatus":0,"count":0},"isSelf":false,"content":"Go Gin框架Web应用embed打包静态文件和模板文件

目录结构

Go Gin框架Web应用embed打包静态文件和模板文件

main.go主文件代码

package mainnnimport (nt"embed"nt"github.com/gin-gonic/gin"nt"html/template"nt"io/fs"nt"net/http"n)n//使用go:embed static template定义静态文件目录和模板文件目录n//这里的//go:embed static template并不是注释的意思n//go:embed static templatenvar f embed.FSnnfunc main() {nntapp := gin.Default()nt//设置模板文件目录nttemple := template.Must(template.New("x").ParseFS(f, "template/**/*"))ntapp.SetHTMLTemplate(temple)nt//设置静态文件目录ntfp, _ := fs.Sub(f, "static")ntapp.StaticFS("/static", http.FS(fp))nt//GET请求,“/”ntapp.GET("/", func(c *gin.Context) {nttc.HTML(200, "index/index", nil)nt})nt//GET请求,“/admin”ntapp.GET("/admin", func(c *gin.Context) {nttc.HTML(200, "admin/index", nil)nt})nt//运行nt_ = app.Run("127.0.0.1:80")n}n

Go Gin框架Web应用embed打包静态文件和模板文件

演示

","imageList":["https://p6.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/787843e656c748fd854b96e21a95cc49?from=pc","https://p6.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/cb76eddd62614a89a70a5ca37a79374b?from=pc","https://p6.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/94125d67c1824253b5c9de8bdb23faa6?from=pc"],"mediaInfo":{"userId":"MS4wLjABAAAAZAfgJtDvABnYw2qB7Gb16eCxcjHRH_5WljBAJdLHoUk","unsafeUserId":"52275191954","name":"GoPython","avatarUrl":"https://p3.toutiaoimg.com/origin/user-avatar/e4911db75330de5b370215f407f70f1b","description":"一位专注python和golang知识分享的跨界会计师","userVerified":1,"userAuthInfo":{"auth_type":"3","auth_info":"河南花花牛乳业集团有限公司会计"}},"seoTDK":{"title":"Go Gin框架Web应用embed打包静态文件和模板文件-今日头条","description":"目录结构main.go主文件代码package mainimport (t"embed"t"github.","keywords":"HTML","publishTimestamp":"1656044289","modifiedTimestamp":"1656045503"},"logId":"202206241332060101581860390693BABF","sylpageConfig":{"card":{"id":""}},"identity":{"web_id":"7111199547455817229","user_is_login":false},"abtestInfo":{"rsp_type":5,"version_name":"4252807,4164637","parameters":{"feat_repost_type":{"new":true},"home_nav_conf":{"dcd_out":1},"local_filter":{"core_filter":{"filter_list":{"ms::TicaiFilter":true}}},"page_upgrade":{"new_profile":true,"video_double_column":true},"sati":{"enable_ad_prime":true,"enable_sorter_optimus":true,"prime_rule_rank_version":"toutiao_web","use_toutiao_web_feed":true,"format_max_consecutive_middle":2,"format_max_consecutive_nogroups":3,"enable_reduce_nogroup":true},"seraph":{"score_rule":{"default":{"replace":{"group_util":"_CTR - 1000*dislike"}},"new_user":{"replace":{"group_util":"_CTR - 1000*dislike"}}}},"sort":{"allowed_ticai":["forum_post","pgc_text","pgc_video"]},"video_detail_page_upgrade":{"new_page":true}},"env_flag":0},"localCityInfo":{"name":"北京","code":"110000","channelId":3202164529},"showResearch":false}}

返回顶部