這篇文章主要介紹了Go語言實(shí)現(xiàn)簡(jiǎn)單留言板的方法,涉及數(shù)據(jù)庫(kù)、模板頁(yè)面元素等留言板相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了Go語言實(shí)現(xiàn)簡(jiǎn)單留言板的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼代碼如下:
package main
import (
// "fmt"
"io"
"log"
"net/http"
"text/template"
"time"
"database/sql"
"github.com/ziutek/mymysql/godrv"
)
// 留言結(jié)構(gòu)
type Liuyan struct {
Id int
Name string
Content string
Time int
}
// 顯示留言時(shí)間
func (l Liuyan) ShowTime() string {
t := time.Unix(int64(l.Time), 0)
return t.Format("2006-01-02 15:04:05")
}
func main() {
godrv.Register("SET NAMES utf8")
// 連接數(shù)據(jù)庫(kù)
db, err := sql.Open("mymysql", "tcp:127.0.0.1:3306*go/root/123456")
if err != nil {
panic(err)
}
defer db.Close()
// 準(zhǔn)備模板
tpl, err := template.New("liuyanbook").Parse(html)
if err != nil {
panic(err)
}
// 顯示留言頁(yè)面 /
requestList := func(w http.ResponseWriter, req *http.Request) {
// 查詢數(shù)據(jù)
rows, err := db.Query("select * from liuyan")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// 獲取數(shù)據(jù)
lys := []Liuyan{}
for rows.Next() {
ly := Liuyan{}
err := rows.Scan(&ly.Id, &ly.Name, &ly.Content, &ly.Time)
if nil != err {
log.Fatal(err)
}
lys = append(lys, ly)
}
// 顯示數(shù)據(jù)
err = tpl.ExecuteTemplate(w, "list", lys)
if err != nil {
log.Fatal(err)
}
}
// 留言頁(yè)面 /liuyan
requestLiuyan := func(w http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil{
log.Fatal(err)
}
if "POST" == req.Method {
if len(req.Form["name"]) < 1 {
io.WriteString(w, "參數(shù)錯(cuò)誤!/n")
return
}
if len(req.Form["content"]) < 1 {
io.WriteString(w, "參數(shù)錯(cuò)誤!/n")
return
}
name := template.HTMLEscapeString(req.Form.Get("name"))
content := template.HTMLEscapeString(req.Form.Get("content"))
// sql語句
sql, err := db.Prepare("insert into liuyan(name, content, time) values(?, ?, ?)")
if err != nil {
log.Fatal(err)
}
defer sql.Close()
// sql參數(shù),并執(zhí)行
_, err = sql.Exec(name, content, time.Now().Unix())
if err != nil {
log.Fatal(err)
}
// 跳轉(zhuǎn)
w.Header().Add("Location", "/")
w.WriteHeader(302)
// 提示信息
io.WriteString(w, "提交成功!/n")
return
}
err = tpl.ExecuteTemplate(w, "liuyan", nil)
if err != nil {
log.Fatal(err)
}
}
http.HandleFunc("/", requestList)
http.HandleFunc("/liuyan", requestLiuyan)
err = http.ListenAndServe(":12345", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// 網(wǎng)頁(yè)模板
var html string = `{{define "list"}}{{/* 留言列表頁(yè)面 */}}<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p><a href="/liuyan">給我留言</a></p>
<table>
{{range .}}
<tr>
<td>{{.Id}}</td><td>{{.Name}}</td><td>{{.Content}}</td><td>{{.ShowTime}}</td>
</tr>
{{end}}
</table>
</body>
</html>{{end}}
{{define "liuyan"}}{{/* 發(fā)布留言頁(yè)面 */}}<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form method="post">
姓名:<input type="text" name="name" /><br>
內(nèi)容:<input type="text" name="content" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>{{end}}
希望本文所述對(duì)大家的Go語言程序設(shè)計(jì)有所幫助。