This article is an example of how the go language implements a simple message board. Share to everyone for your reference. The implementation methods are as follows:
Package Main
Import (
"FMT"
"IO"
"Log"
"Net/http"
"Text/template"
"Time"
"Database/sql"
"Github.com/ziutek/mymysql/godrv"
)
Message structure
Type Liuyan struct {
Id int
Name string
Content string
Time int
}
Show Message Time
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")
Connecting to a database
DB, err: = SQL. Open ("Mymysql", "tcp:127.0.0.1:3306*go/root/123456")
If Err!= nil {
Panic (ERR)
}
Defer db. Close ()
Preparing templates
TPL, err: = template. New ("Liuyanbook"). Parse (HTML)
If Err!= nil {
Panic (ERR)
}
Show Message page/
Requestlist: = Func (w http. Responsewriter, req *http. Request) {
Querying data
Rows, err: = db. Query ("SELECT * from Liuyan")
If Err!= nil {
Log. Fatal (ERR)
}
Defer rows. Close ()
Get Data
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)
}
Display data
Err = TPL. Executetemplate (W, "list", Lys)
If Err!= nil {
Log. Fatal (ERR)
}
}
Message page/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, "parameter Error!\n")
Return
}
If Len (req. form["Content"] < 1 {
Io. WriteString (w, "parameter Error!\n")
Return
}
Name: = template. Htmlescapestring (req. Form.get ("name"))
Content: = template. Htmlescapestring (req. Form.get ("content"))
SQL statement
SQL, Err: = db. Prepare ("INSERT into Liuyan (name, content, time) values (?,?,?)")
If Err!= nil {
Log. Fatal (ERR)
}
Defer SQL. Close ()
SQL parameters, and executes
_, Err = sql. Exec (name, content, time. Now (). Unix ())
If Err!= nil {
Log. Fatal (ERR)
}
Jump
W.header (). ADD ("Location", "/")
W.writeheader (302)
Hint Information
Io. WriteString (W, "submitted successfully!\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)
}
}
Page templates
var html string = ' {{define ' list '}}{{/* message list page */}}<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<body>
<p><a href= "/liuyan" > Leave me a message </a></p>
<table>
{{range.}}
<tr>
<td>{{. id}}</td><td>{{. name}}</td><td>{{. content}}</td><td>{{. Showtime}}</td>
</tr>
{{End}}}
</table>
</body>
{{define ' Liuyan '}} {* * Publish message page */}}<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<body>
<form method= "POST" >
Name: <input type= "text" name= "name"/><br>
Content: <input type= "text" name= "content"/><br>
<input type= "Submit" value= "submitted"/>
</form>
</body>
I hope this article will help you with your go language program.