Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Golang网站中使用模板显示mysql表_Mysql_Templates_Go_Web_Struct - Fatal编程技术网

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

在Golang网站中使用模板显示mysql表,mysql,templates,go,web,struct,Mysql,Templates,Go,Web,Struct,我正在学习GoLang web开发,我正在尝试一个简单的示例,在使用GoLang模板呈现的网页上显示mysql数据库中的表。 下面是我的代码片段,它不起作用。表呈现失败,其中模板处的range函数不起作用 Server.go package main import ( "fmt" "net/http" _ "github.com/go-sql-driver/mysql" "database/sql" "os" "html/template" )

我正在学习GoLang web开发,我正在尝试一个简单的示例,在使用GoLang模板呈现的网页上显示mysql数据库中的表。 下面是我的代码片段,它不起作用。表呈现失败,其中模板处的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 }}
{{定义“索引”}
迈塔南网站
mysql的Maithanamm网站
{{$length:=len.}
长度{{$Length}
名字
第二名
部门
电子邮件
{{range.}}
{{.fname}
{{.sname}
{{.dname}
{{.email}
{{end}
{{end}
网页 您有两个问题:

  • 调用
    ExecuteTemplate
    时没有检查错误
  • 您的模板使用的是非导出字段,处理好第一个问题会告诉您这一点
  • 关于第一个问题:

    err := tmpl.ExecuteTemplate(w, "Index", table)
    if err != nil {
        // Do something with the error
    }
    
    对于第二个问题,请将
    结构更改为具有导出字段:

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

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

    员工字段名必须为空。ExecuteTemplate返回的错误是什么?错误返回值当前已被忽略。非常感谢…我从未想过即使是变量大小写也有一些值…很高兴知道…您可能希望再次运行,导出名称的大小写很快就会出现。