2019-04-25 13:41
浏览 448


I want to create an application that will call a boolean function and depending on the result provide 1 of 2 compiled react apps as static sites.

I'm using the LoadHTMLGlob function recommended by gin and it works fine with .tmpl files like the example in thier docs. However when doing just static html with a static directory for each site nothing seems to go well.

File Structure:

├── main.go
└── sites
    ├── new
    │   ├── index.html
    │   └── static
    └── old
        ├── index.html
        └── static

Go Code:

func main() {
    r := gin.Default()
    //r.LoadHTMLFiles("sites/old/index.html", "sites/new/index.html") //doesn't complain, but can't load html
    r.LoadHTMLGlob("sites/**/*") // complains about /static being a dir on boot
    r.GET("/sites/lib", func(c *gin.Context) {
        id := c.Query("id")
        useNewSite, err := isBetaUser(id)
        if err != nil {
            c.AbortWithStatusJSON(500, err.Error())
        if useNewSite {
            c.HTML(http.StatusOK, "new/index.html", nil)
        } else {
            c.HTML(http.StatusOK, "old/index.html", nil)
    routerErr := r.Run(":8080")
    if routerErr != nil {

I expect that when isBetaUser comes back as true it should load the static content under sites/new otherwise load sites/old.

However loading globs produces: panic: read sites/new/static: is a directory when starting panics.

Loading the html files individually (commented out above) Runs fine, but when a request comes it panics with:

html/template: "new/index.html" is undefined

I've also string using sites/[old||new]/index.html in c.HTML

图片转代码服务由CSDN问答提供 功能建议

我想创建一个将调用布尔函数的应用程序,并根据结果提供2个已编译的React应用程序中的1个 作为静态站点。

我正在使用gin推荐的LoadHTMLGlob函数,它可以与.tmpl文件(如其文档中的示例)配合使用。 但是,当只对每个站点使用静态html静态目录时,似乎没有任何进展。


 ├──  main.go 
└── 静态


  func main(){  
r:= gin.Default()
 //r.LoadHTMLFiles("sites/old/index.html“,” sites / new / index.html“)//没有抱怨,但无法加载html  
 r.LoadHTMLGlob(“ sites / ** / *”)//抱怨/ static是启动时的目录
 r.GET(“ / sites / lib”,func(c * gin.Context){
  id:= c.Query(“ id”)
 useNewSite,err:= isBetaUser(id)
如果err!= nil {
 if useNewSite {
 c.HTML(http.StatusOK,“ new / index.html”,nil)
c  .HTML(http.StatusOK,“ old / index.html”,nil)
 routerErr:= r.Run(“:8080”)
 if routerErr!= nil {
紧急情况(  routerErr.Error())

我希望当isBetaUser返回为true时,应该将静态内容加载到site / new下,否则加载 站点/旧。

但是加载全局变量会产生: 恐慌:读取站点/新/静态:是目录 在开始出现紧急情况时。

单独加载html文件(上面已注释) 运行正常,但是当请求到来时,它会出现以下问题:

  html / template:“新 /index.html”是不确定的

我也在c.HTML中使用site / [old || new] /index.html进行了字符串

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • donglue8180 2019-04-25 14:55

    Try sites/**/*.html to fix the panic.

    And note that Go uses the template files' base name as the template name, so to execute a template you don't use "path/to/template.html" but "template.html". This, of course, causes a problem in your case since as explained in the documentation:

    When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results.

    To fix this you need to explicitly name your templates which you can do by using the {{ define "template_name" }} action.

    1. Open sites/new/index.html
    2. Add {{ define "new/index.html" }} as the first line
    3. Add {{ end }} as the last line
    4. Repeat for sites/old/index.html with "old/index.html" as the name.
    解决 无用
    打赏 举报
  • dongpaocuan7498 2019-04-25 14:51

    You need to define the template in your template files first, whether it's html/tmpl file. Something like this,

    {{ define "new/index.tmpl" }} ... {{ end }}

    or if you want to stick with html file then it would be

    {{ define "new/index.html" }} ... {{ end }}.

    So your template file (from your example: sites/new/index.html) should look like this,

    {{ define "new/index.html" }}
             {{ .title }}
         <p>New site</p>
    {{ end }}
    解决 无用
    打赏 举报

相关推荐 更多相似问题