最近學習golang也有一段時間了,基礎差很少學了個大概,由於本人是java程序員,因此對web更感興趣。根據《go web編程》中的例子改編一個更簡單的例子,供新手參考,廢話很少說,上菜:html
這個例子使用到了beego框架和beedb框架,若是是go新手beego和beedb得本身去google下載安裝。java
目錄結構:mysql
index.gogit
package controllers import ( "fmt" "github.com/astaxie/beego" "login/models" ) type IndexController struct { beego.Controller } func (index *IndexController) Get() { sess := index.StartSession() username := sess.Get("username") fmt.Println(username) if username == nil || username == "" { index.TplNames = "index.tpl" } else { index.TplNames = "success.tpl" } } func (index *IndexController) Post() { sess := index.StartSession() var user models.User inputs := index.Input() //fmt.Println(inputs) user.Username = inputs.Get("username") user.Pwd = inputs.Get("pwd") err := models.ValidateUser(user) if err == nil { sess.Set("username", user.Username) fmt.Println("username:", sess.Get("username")) index.TplNames = "success.tpl" } else { fmt.Println(err) index.TplNames = "error.tpl" } }
regist.go程序員
package controllers import ( "fmt" "github.com/astaxie/beego" "login/models" ) type RegistController struct { beego.Controller } func (this *RegistController) Get() { this.TplNames = "regist.tpl" } func (this *RegistController) Post() { var user models.User inputs := this.Input() //fmt.Println(inputs) user.Username = inputs.Get("username") user.Pwd = inputs.Get("pwd") err := models.SaveUser(user) if err == nil { this.TplNames = "success.tpl" } else { fmt.Println(err) this.TplNames = "error.tpl" } }
models.gogithub
package models import ( "database/sql" "errors" "fmt" "github.com/astaxie/beedb" _ "github.com/ziutek/mymysql/godrv" ) type User struct { Id int `PK` Username string Pwd string } func getLink() beedb.Model { db, err := sql.Open("mysql", "root:root@tcp(192.168.1.81:3306)/test_my?charset=utf8") if err != nil { panic(err) } orm := beedb.New(db) return orm } func SaveUser(user User) error { orm := getLink() fmt.Println(user) err := orm.Save(&user) return err } func ValidateUser(user User) error { orm := getLink() var u User orm.Where("username=? and pwd=?", user.Username, user.Pwd).Find(&u) if u.Username == "" { return errors.New("用戶名或密碼錯誤!") } return nil }
main.gogolang
package main import ( //"fmt" "github.com/astaxie/beego" "login/controllers" ) type MainController struct { beego.Controller } func main() { beego.SessionOn = true beego.RegisterController("/", &controllers.IndexController{}) beego.RegisterController("/regist", &controllers.RegistController{}) beego.Run() }
error.tpl web
<html> <body> error </body> </html>
success.tplsql
<html> <body> successed </body> </html>
index.tpl數據庫
<html> <body> <h1>login</h1> <form action="/" method="post"> 用戶名:<input type="text" name="username"><br> 密碼:<input type="password" name="pwd"><br> <input type="submit" value="登陸"><br> <a href="/regist">註冊</a> </form> </body> </html>
regist.tpl
<html> <body> <h1>regist</h1> <form action="/regist" method="post"> 用戶名:<input type="text" name="username"><br> 密碼:<input type="password" name="pwd"><br> <input type="submit" value="註冊"> </form> </body> </html>
數據庫用的是mysql,建表語句
CREATE TABLE user ( id INT, username VARCHAR(32), pwd VARCHAR(32), PRIMARY KEY(id) );
程序跑起來是沒有問題,可是session總是取不到數據,求大神解釋!