title: error calling eq: invalid type for comparison in Go template
body: <p>Background: a few static webpages share the same header. I am going to highlight the active header item by judging its state (in the Go template), with </p>
  
  <pre><code>&lt;a href="/home" class="{{ if eq .Active "home"}} active{{end}}"&gt;Home&lt;/a&gt;
  </code></pre>
  
  <p>which, in the terminal, ended up with </p>
  
  <pre><code>2018/08/19 16:46:49 template: _header.html:21:53: executing "_header.html" at &lt;eq . "home"&gt;: error calling eq: invalid type for comparison
  </code></pre>
  
  <p>The error basically complained about undefined "<code>.Active</code>", which made me believe in failure with loading view models.<br/>
  Here is the <a href="https://github.com/IdiotLeon/TutorialCreatingWebApplicationWithGo/commit/5d4aed46596b17ff1dc4d10a70db9f7b3dfd10f5" rel="nofollow noreferrer">commit</a> and <a href="https://github.com/IdiotLeon/TutorialCreatingWebApplicationWithGo" rel="nofollow noreferrer">repo</a>. And I will show key files below:</p>
  
  <p>_layout.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;
      &lt;title&gt;{{.Title}}&lt;/title&gt;
      &lt;link href="/css/app.css" rel="stylesheet" /&gt; {{block "styles" .}}{{end}}
  &lt;/head&gt;
  
  &lt;body&gt;
      {{template "_header.html"}} {{template "content" .}} {{template "_footer.html"}}{{block "scripts" .}}{{end}}
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>_header.html</p>
  
  <pre><code>&lt;header class="row"&gt;
      &lt;div class="columns small-2"&gt;
          &lt;div class="logo-container"&gt;
              &lt;div class="logo"&gt;&lt;/div&gt;
              &lt;h1&gt;Lemonade Stand Supply&lt;/h1&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="columns small-10"&gt;
          &lt;div class="row"&gt;
              &lt;div class="columns small-1 small-offset-9"&gt;
                  &lt;a class="secondary-color tiny" href="/login"&gt; login &lt;/a&gt;
              &lt;/div&gt;
              &lt;div class="columns small-2"&gt;
                  &lt;img src="/img/cart_small.png" alt="cart" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="row"&gt;
              &lt;nav class="columns small-6 small-offset-6"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="columns small-3 item"&gt;
                          &lt;a href="/home" class="{{ if eq .Active "home"}} active {{end}}"&gt;Home&lt;/a&gt;
                      &lt;/div&gt;
                      &lt;div class="columns small-3 item"&gt;
                          &lt;a href="/shop" class="{{ if eq .Active "shop"}} active {{end}}"&gt;Shop&lt;/a&gt;
                      &lt;/div&gt;
                      &lt;div class="columns small-6 item"&gt;
                          &lt;a href="." class="{{ if eq .Active "standlocator"}} active {{end}}"&gt;Stand Locator&lt;/a&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/nav&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/header&gt;
  </code></pre>
  
  <p>main.go</p>
  
  <pre><code>package main
  
  import (
      "html/template"
      "io/ioutil"
      "log"
      "net/http"
      "os"
  
      "TutorialCreatingWebApplicationsWithGo/src/github.com/lss/webapp/viewmodel"
  )
  
  func main() {
      templates := populateTemplates()
      http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
          requestedFile := r.URL.Path[1:]
          template := templates[requestedFile+".html"]
          var context interface{}
          switch requestedFile {
          case "shop":
              context = viewmodel.NewShop()
          default:
              context = viewmodel.NewHome()
          }
          if template != nil {
              err := template.Execute(w, context)
              if err != nil {
                  log.Println(err)
              }
          } else {
              w.WriteHeader(http.StatusNotFound)
          }
      })
      http.Handle("/img/", http.FileServer(http.Dir("../../../../public")))
      http.Handle("/css/", http.FileServer(http.Dir("../../../../public")))
      http.ListenAndServe(":8080", nil)
  }
  
  func populateTemplates() map[string]*template.Template {
      result := make(map[string]*template.Template)
      const basePath = "../../../../templates"
      layout := template.Must(template.ParseFiles(basePath + "/_layout.html"))
      template.Must(layout.ParseFiles(basePath+"/_header.html", basePath+"/_footer.html"))
      dir, err := os.Open(basePath + "/content")
      if err != nil {
          panic("Failed to open template blocks directory: " + err.Error())
      }
      fis, err := dir.Readdir(-1)
      if err != nil {
          panic("Failed to read contents of content directory: " + err.Error())
      }
      for _, fi := range fis {
          f, err := os.Open(basePath + "/content/" + fi.Name())
          if err != nil {
              panic("Failed to open template '" + fi.Name() + "'")
          }
          content, err := ioutil.ReadAll(f)
          if err != nil {
              panic("Failed to read content from file '" + fi.Name() + "'")
          }
          f.Close()
          tmpl := template.Must(layout.Clone())
          _, err = tmpl.Parse(string(content))
          if err != nil {
              panic("Failed to parse contents of '" + fi.Name() + "' as template")
          }
          result[fi.Name()] = tmpl
      }
      return result
  }
  </code></pre>
  
  <p>view model - home.go</p>
  
  <pre><code>package viewmodel
  
  type Home struct {
      Title  string
      Active string
  }
  
  func NewHome() Home {
      result := Home{
          Active: "home",
          Title:  "Lemonade Stand Supply",
      }
      return result
  }
  </code></pre>
  
  <p>a view model - shop.go</p>
  
  <pre><code>package viewmodel
  
  type Shop struct {
      Title      string
      Active     string
      Categories []Category
  }
  
  type Category struct {
      URL         string
      ImageURL    string
      Title       string
      Description string
  }
  
  func NewShop() Shop {
      result := Shop{
          Title:  "Lemonade Stand Supply - Shop",
          Active: "shop",
      }
      juiceCategory := Category{
          URL:      "/shop_details",
          ImageURL: "lemon.png",
          Title:    "Juices and Mixes",
          Description: `Explore our wide assortment of juices and mixes expected by today's
          lemonade stand clientelle. Now featuring a full line of organic juices that are guaranceed
          to be obtained from trees that have never been treated with pesticide or artificial fertilizers.`,
      }
      supplyCategory := Category{
          URL:      ".",
          ImageURL: "kiwi.png",
          Title:    "Cups, Straws, and Other Supplies",
          Description: `From paper cups to bio-degradable plastic to straws and napkins,
          LSS is your source for the sundries that keep you stand running smoothl.`,
      }
      advertiseCategory := Category{
          URL:      ".",
          ImageURL: "pineapple.png",
          Title:    "Signs and Advertising",
          Description: `Sure, you could just wait for people to find your stand along
          the side of the road, but if you want to take it to the next level, our premium
          line of advertising supplies.`,
      }
      result.Categories = []Category{juiceCategory, supplyCategory, advertiseCategory}
      return result
  }
  </code></pre>
  
  <p>This is literal follow up with the course <a href="https://app.pluralsight.com/library/courses/creating-web-applications-go-update/table-of-contents" rel="nofollow noreferrer"><em>Creating Web Application</em> with Go by Mike Van Sickle</a> (module 5.7, <a href="https://app.pluralsight.com/player?course=creating-web-applications-go-update&amp;author=mike-vansickle&amp;name=creating-web-applications-go-update-m4&amp;clip=7&amp;mode=live" rel="nofollow noreferrer">Logical Tests</a>) . Having carefully matched each file with the example code provided by the lecturer (which worked perfectly fine), I found the code were the same and still cannot find the cause to the problem.</p>