Description

I am following a book Building Distributed Applications in Gin by Mohamed Labouardy . In the book was introduced a way to bundle the template and static files using go-assets-builder . My issue is that I am not able import the Assets variable that is in the generated file by the go-assets-builder command. I saw the same thing documented on the gin page https://gin-gonic.com/docs/examples/bind-single-binary-with-template/ . I have also spotted on stack overflow https://stackoverflow.com/questions/47512171/unable-to-load-html-templates-with-gin/59810993#59810993 that there supposed to be a specific folder structure but not sure how much of that is true. Like shown on the picture below my IDE isn't able to find reference to the variable .

Assets variable not getting resolved.

Screenshot 2023-10-18 at 06 48 06

relevent content of assets.go showing definition of the Assets variable

Screenshot 2023-10-18 at 23 17 54

How to reproduce


├── 404.html
├── assets
│   ├── css
│   │   └── app.css
│   └── images
│       ├── 404.jpg
│       ├── .....
├── cmd
│   └── client
│       └── assets.go
├── distributed-golang-webpages.iml
├── go.mod
├── go.sum
├── index.html
├── main.go
├── recipes.json
└── templates
    ├── index.tmpl
    ├── navbar.tmpl
    └── recipe.tmpl

go-assets-builder templates assets 404.html recipes.json -o cmd/client/assets.go 

// main.go
package main

func loadTemplate() (*template.Template, error) {
    t := template.New("")

    for name, file := range Assets.Files {
        if file.IsDir() || !strings.HasSuffix(name, ".tmpl") {
            continue
        }
        h, err := ioutil.ReadAll(file)
        if err != nil {
            return nil, err
        }
        t, err = t.New(name).Parse(string(h))
        if err != nil {
            return nil, err
        }
    }
    return t, nil
}


func main() {
    t, err := loadTemplate()
    if err != nil {
        panic(err)
    }
    router := gin.Default()
    router.SetHTMLTemplate(t)
    router.GET("/", IndexHandler)
    router.GET("/recipes/:id", RecipeHandler)
    router.GET("/assets/*filepath", StaticHandler)
    router.Static("/assets", "./assets")
    router.StaticFile("/favicon.ico", "./assets/images/favicon.ico")
    //router.LoadHTMLGlob("templates/*")
    router.Run()
}

Not really sure where to go from here. Grateful if someone can throw some more light on this. Thanks in advance

Environment

  • go version: go1.21.1
  • gin version (or commit ref): v1.9.1
  • operating system: MacBook 14.0
  • external library: github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15