首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Golang网站中使用模板显示mysql表

在Golang网站中使用模板显示mysql表
EN

Stack Overflow用户
提问于 2018-06-09 05:21:53
回答 1查看 2.3K关注 0票数 3

我在学习GoLang网络开发的过程中,我正在尝试一个简单的例子显示在Golang数据库中的表格在网页上使用Golang模板呈现。下面是我的代码片段,这不是working.Table渲染失败,其中range函数在模板上不起作用

Server.go

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(&first_name, &last_name, &department, &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)
    }
}

layout.html

{{ define "Index" }}
<!DOCTYPE html>
<html lang="en-US">
    <head>
        <title>Maithanam Website</title>
        <meta charset="UTF-8" />
    </head>
    <body>
        <h1>Maithanammm website for mysql</h1> 

    {{ $length := len . }}
    <h3>Length {{$length}}</h3> 

    <table border="1">
      <tr>
        <td>FirstName</td>
        <td>SecondName</td>
        <td>Department</td>
        <td>Email</td>
      </tr>
    {{ range . }}
      <tr>
        <td>{{ .fname }}</td>
        <td> {{ .sname }} </td>
        <td>{{ .dname }} </td>
        <td>{{ .email }} </td>
      </tr>
    {{ end }}
    </table>

    </body>
</html>
{{ end }}

WebPage enter image description here

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-09 06:15:10

您有两个问题:

如果您在调用ExecuteTemplate.

  • Your

  • 时没有检查错误模板是否使用了未导出的字段,那么解决第一个问题会告诉您这一点。

对于第一个问题:

err := tmpl.ExecuteTemplate(w, "Index", table)
if err != nil {
    // Do something with the error
}

对于第二个问题,将您的struct更改为具有导出字段:

type Employee struct {
    Fname, Sname, Dname, Email string
}

然后更改模板以使用新的字段名称。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50768543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档