title: Display mysql table using template in Golang website
body: <p>I am in the process learning GoLang web development, where I am trying a simple example of display a table in mysql database at Webpage rendered using Golang template.
  Below is the snippet of my code, which is not working.Table rendering is failing,where <strong>range</strong> function at template not working</p>
  
  <p>Server.go</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "net/http"
      _ "github.com/go-sql-driver/mysql"
      "database/sql"
      "os"
      "html/template"
  )
  
  type Employee struct {
      fname, sname, dname, email string
  }
  
  func helloWorld(w http.ResponseWriter, r *http.Request){
      name, err := os.Hostname()
      checkErr(err)
      fmt.Fprintf(w, "HOSTNAME : %s\n", name)
  }
  
  func dbConnect() (db *sql.DB) {
      dbDriver := "mysql"
      dbUser := "root"
      dbPass := "password"
      dbHost := "mysql.go"
      dbPort := "3306"
      dbName := "company"
      db, err := sql.Open(dbDriver, dbUser +":"+ dbPass +"@tcp("+ dbHost +":"+ dbPort +")/"+ dbName +"?charset=utf8")
      checkErr(err)
      return db
  }
  
  func dbSelect() []Employee{
      db := dbConnect()
      rows, err := db.Query("select * from employees")
      checkErr(err)
  
      employee := Employee{}
      employees := []Employee{}
  
      for rows.Next() {
          var first_name, last_name, department, email string
          err = rows.Scan(&amp;first_name, &amp;last_name, &amp;department, &amp;email)
          checkErr(err)
          employee.fname = first_name
          employee.sname = last_name
          employee.dname = department
          employee.email = email
          employees = append(employees, employee)
  
      }
      defer db.Close()
      return employees
  }
  
  var tmpl = template.Must(template.ParseFiles("layout.html"))
  //var tmpl = template.Must(template.ParseGlob("layout.html"))
  func dbTableHtml(w http.ResponseWriter, r *http.Request){
      table := dbSelect()
      tmpl.ExecuteTemplate(w, "Index", table)
  }
  
  func dbTable(w http.ResponseWriter, r *http.Request){
      table := dbSelect()
      for i := range(table) {
          emp := table[i]
          fmt.Fprintf(w,"YESS|%12s|%12s|%12s|%20s|\n" ,emp.fname ,emp.sname ,emp.dname ,emp.email)
      }
  }
  
  func main() {
      http.HandleFunc("/", helloWorld)
      http.HandleFunc("/view", dbTableHtml) 
      http.HandleFunc("/raw", dbTable)
      http.ListenAndServe(":8080", nil)
  }
  
  func checkErr(err error) {
      if err != nil {
          panic(err)
      }
  }
  </code></pre>
  
  <p><strong>layout.html</strong></p>
  
  <pre><code>{{ define "Index" }}
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en-US"&gt;
      &lt;head&gt;
          &lt;title&gt;Maithanam Website&lt;/title&gt;
          &lt;meta charset="UTF-8" /&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Maithanammm website for mysql&lt;/h1&gt; 
  
      {{ $length := len . }}
      &lt;h3&gt;Length {{$length}}&lt;/h3&gt; 
  
      &lt;table border="1"&gt;
        &lt;tr&gt;
          &lt;td&gt;FirstName&lt;/td&gt;
          &lt;td&gt;SecondName&lt;/td&gt;
          &lt;td&gt;Department&lt;/td&gt;
          &lt;td&gt;Email&lt;/td&gt;
        &lt;/tr&gt;
      {{ range . }}
        &lt;tr&gt;
          &lt;td&gt;{{ .fname }}&lt;/td&gt;
          &lt;td&gt; {{ .sname }} &lt;/td&gt;
          &lt;td&gt;{{ .dname }} &lt;/td&gt;
          &lt;td&gt;{{ .email }} &lt;/td&gt;
        &lt;/tr&gt;
      {{ end }}
      &lt;/table&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  {{ end }}
  </code></pre>
  
  <p><strong>WebPage</strong>
  <a href="https://i.stack.imgur.com/vQGeL.png" rel="nofollow noreferrer">enter image description here</a></p>