佳木斯湛栽影视文化发展公司

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

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

熱門標(biāo)簽:百度競(jìng)價(jià)排名 呼叫中心市場(chǎng)需求 服務(wù)外包 網(wǎng)站排名優(yōu)化 Linux服務(wù)器 AI電銷 地方門戶網(wǎng)站 鐵路電話系統(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ù)庫
    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)
    }
   
    // 顯示留言頁面 /
    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)
        }
    }
   
    // 留言頁面 /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)頁模板
var html string = `{{define "list"}}{{/* 留言列表頁面 */}}!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ā)布留言頁面 */}}!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)簽:湖南 衡水 銅川 蘭州 仙桃 黃山 崇左 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Go語言實(shí)現(xiàn)簡(jiǎn)單留言板的方法》,本文關(guā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)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    孟州市| 岗巴县| 敦化市| 福贡县| 宜宾市| 永靖县| 苍溪县| 宜都市| 建阳市| 平泉县| 汉中市| 洞口县| 洛宁县| 怀仁县| 长宁县| 临江市| 阳西县| 平陆县| 东莞市| 河池市| 庆阳市| 通榆县| 巴彦淖尔市| 郁南县| 金昌市| 眉山市| 阿克苏市| 天水市| 河东区| 岱山县| 海盐县| 外汇| 毕节市| 平邑县| 张家口市| 普定县| 寿宁县| 岗巴县| 盐源县| 虞城县| 柳河县|