title: How do you use Go 1.16 embed features in subfolders/packages?
body: <p>Go 1.16 is out and I want to use the new embed features. I can get it to work if everything is in the main package. But it's not clear how to handle accessing resources from subfolders/packages. Trying to do it with embed.FS support.</p>
  <p>e.g. I have a main.go and also an HTTP handler in a handlers package/folder</p>
  <p>If I put the handler in the main, it works. If I put it in the handlers package, it can't find the templates. I get:</p>
  <p><code>handlers/indexHandler.go:11:12: pattern templates: no matching files found exit status 1</code></p>
  <p>Similarly, I can get it to serve an image from the static folder if I serve it from /. But I can't serve both a handler from / and the static/images from /. If I put images on /static/ it can't find the images.</p>
  <p>I think it has to do with relative paths. But I can't find the right combination through trial and error... Could it be too early to rely on these features?</p>
  <p>Previously I was using go-rice and I did not have these problems. But I would like to use the std library as much as possible.</p>
  <p>main.go:</p>
  <pre><code>package main
  
  import (...)
  
  //go:embed static
  var static embed.FS
  
  func main() {
  
      fsRoot, _ := fs.Sub(static, &quot;static&quot;)
      fsStatic := http.FileServer(http.FS(fsRoot))
      http.Handle(&quot;/&quot;, fsStatic)
      http.HandleFunc(&quot;/index&quot;, Index)
      http.ListenAndServe(&quot;:8080&quot;, nil)
  }
  </code></pre>
  <p>handlers/indexHandler.go:</p>
  <pre><code>package handlers
  
  import (...)
  
  //go:embed templates
  var templates embed.FS
  
  // Index handler
  func Index(w http.ResponseWriter, r *http.Request) {
  
      tmpl := template.New(&quot;&quot;)
      var err error
      if tmpl, err = tmpl.ParseFS(templates, &quot;simple.gohtml&quot;); err != nil {
          fmt.Println(err)
      }
      if err = tmpl.ExecuteTemplate(w, &quot;simple&quot;, nil); err != nil {
          log.Print(err)
      }    
  }
  </code></pre>
  <p>Structure is as follows...</p>
  <pre><code>.
  ├── go.mod
  ├── go.sum
  ├── handlers
  │   └── indexHandler.go
  ├── main.go
  ├── static
  │   ├── css
  │   │   └── layout.css
  │   └── images
  │       └── logo.png
  └── templates
      └── simple.gohtml
  </code></pre>