This is a creation in Article, where the information may have evolved or changed.
Made a simple message board system, practice practiced hand, used to 7 bags, hehe. Write more and write something later, so progress is faster.
Main.go
Golang Simple Message board System//Author:dotcoo Zhao//mysql//CREATE TABLE Liuyan (//ID int primary KEY auto_increment not null,/ /name varchar () not null,//URL varchar (+) not null,//content varchar (+) not null,//time int//) ENGINE = MYISAM CHARACTER SET UTF8 COLLATE utf8_general_ci; sqlite3//CREATE TABLE Liuyan (//ID integer primary key autoincrement,//name text,//URL text,//cont ENT text,//time integer//);p ackage main import ("Database/sql" "io" "net/http" "Strings" Text/templ Ate "time"//"Github.com/ziutek/mymysql/godrv" _ "Github.com/mattn/go-sqlite3")//message structure type Liuyan struct { Id int Name String URL string Content string Time int}////Display message Duration//func (L Liuyan) ShowTime ( ) string {//T: = time. Unix (Int64 (L.time), 0)//return T.format ("2006-01-02 15:04:05")//} func ShowTime (Timeunix int) string {t: = time. Unix (Int64 (Timeunix), 0) return T.format ("2006-01-02 15: 04:05 ")}//global variable var db *sql. Dbvar View *template. Template Func Main () {//Godrv. Register ("SET NAMES UTF8")//Connect database var err//DB, err: = SQL. Open ("Mymysql", "tcp:127.0.0.1:3306*go/root/123456") db, err = sql. Open ("Sqlite3", "./liuyan.db") if err! = Nil {panic (ERR)} defer db. Close ()//Prepare template err = LoadTemplate () if err! = Nil {panic (ERR)}//Register handler for HTTP. Handlefunc ("/load", Loadhandler) http. Handlefunc ("/", Listhandler) http. Handlefunc ("/liuyan", Liuyanhandler)//start Server Err = http. Listenandserve (": 12345", nil) if err! = Nil {panic (err)}}//Load template func loadtemplate () error {//Prepare template function Funcs: = Make (template. FUNCMAP) funcs["Showtime"] = Showtime//Prepare template V: = template. New ("View") V.funcs (Funcs) _, Err: = V.parseglob ("view/*.htm") if err! = Nil {return err} view = V return nil}//dynamic load Template/loadfunc Loadhandler (w http. Responsewriter, req *http. Request) { ERR: = LoadTemplate () if err! = Nil {http. Error (W, err. Error (), () return} io. WriteString (w, ' template loading complete ')}//Display Message page/func listhandler (w http. Responsewriter, req *http. Request) {//query data rows, err: = db. Query ("SELECT * from Liuyan") if err! = Nil {http. Error (W, err. Error (), $) return} defer rows. Close ()//Get data lys: = []liuyan{} for rows. Next () {ly: = liuyan{} ERR: = rows. Scan (&ly. Id, &ly. Name, &ly. URL, &ly. Content, &ly. TIME) if nil! = Err {http. Error (W, err. Error (), +) return} Lys = Append (Lys, ly)}//Display data Err = view. Executetemplate (W, "index.htm", Lys) if err! = Nil {http. Error (W, err. Error (), () return}}//Message page/liuyanfunc Liuyanhandler (w http. Responsewriter, req *http. Request) {if "POST" = = req. Method {//get parameter name: = Strings. Trimspace (req. Formvalue ("name")) URL: = StriNgs. Trimspace (req. Formvalue ("url")) Content: = Strings. Trimspace (req. Formvalue ("content")//check parameter if name = = "" | | Content = = "" {io. WriteString (w, "parameter Error!\n") return}//SQL statement SQL, ERR: = db. Prepare ("INSERT into Liuyan (name, URL, content, time) VALUES (?,?,?,?)") If err! = Nil {http. Error (W, err. Error (), +) return} defer SQL. Close ()//SQL parameter, and execute _, err = sql. Exec (name, URL, content, time. Now (). Unix ()) if err! = Nil {http. Error (W, err. Error (), () return}//Jump W.header (). ADD ("Location", "/") W.writeheader (302)//cue information io. WriteString (W, "Submit success!\n") return}//Show form err: = view. Executetemplate (W, "liuyan.htm", nil) if err! = Nil {http. Error (W, err. Error (), +) return}}
View/index.htm
<! DOCTYPE html>
View/liuyan.htm
<! DOCTYPE html>