主頁(yè) > 知識(shí)庫(kù) > Go語言實(shí)現(xiàn)簡(jiǎn)單留言板的方法

Go語言實(shí)現(xiàn)簡(jiǎn)單留言板的方法

熱門標(biāo)簽:滁州自建外呼系統(tǒng) 智能營(yíng)銷軟件 工商信用卡外呼系統(tǒng)教程 經(jīng)常接到推銷電話機(jī)器人的電話 客服級(jí)電銷機(jī)器人 海外照相館地圖標(biāo)注入駐 外呼系統(tǒng)如何接收服務(wù)密碼 旅游廁所如何電子地圖標(biāo)注 外呼系統(tǒng)多少錢一年

本文實(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ì)有所幫助。

您可能感興趣的文章:
  • js 實(shí)現(xiàn)的可折疊留言板(附源碼下載)
  • 本人ajax留言板的源程序 不錯(cuò)的應(yīng)用js
  • php簡(jiǎn)單的留言板與回復(fù)功能具體實(shí)現(xiàn)
  • php開發(fā)留言板的CRUD(增,刪,改,查)操作
  • 一個(gè)簡(jiǎn)單的PHP&MYSQL留言板源碼
  • ASP+XML留言板介紹
  • 利用XML開發(fā)留言板簡(jiǎn)單的例子
  • JS+CSS模擬可以無刷新顯示內(nèi)容的留言板實(shí)例

標(biāo)簽:晉城 湘潭 運(yùn)城 楚雄 喀什 本溪 九江 深圳

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Go語言實(shí)現(xiàn)簡(jiǎn)單留言板的方法》,本文關(guān)鍵詞  語言,實(shí)現(xiàn),簡(jiǎn)單,留言板,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Go語言實(shí)現(xiàn)簡(jiǎn)單留言板的方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Go語言實(shí)現(xiàn)簡(jiǎn)單留言板的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章