這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
最近學習golang也有一段時間了,基礎差不多學了個大概,因為本人是java程式員,所以對web更感興趣。根據《go web編程》中的例子改編一個更簡單的例子,供新手參考,廢話不多說,上菜:
這個例子使用到了beego架構和beedb架構,如果是go新手beego和beedb得自己去google下載安裝。
目錄結構:
index.go
package controllersimport ("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.Userinputs := 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 controllersimport ("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.Userinputs := 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.go
package modelsimport ("database/sql""errors""fmt""github.com/astaxie/beedb"_ "github.com/ziutek/mymysql/godrv")type User struct {Id int `PK`Username stringPwd 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 Userorm.Where("username=? and pwd=?", user.Username, user.Pwd).Find(&u)if u.Username == "" {return errors.New("使用者名稱或密碼錯誤!")}return nil}
main.go
package mainimport (//"fmt""github.com/astaxie/beego""login/controllers")type MainController struct {beego.Controller}func main() {beego.SessionOn = truebeego.RegisterController("/", &controllers.IndexController{})beego.RegisterController("/regist", &controllers.RegistController{})beego.Run()}
error.tpl
<html><body> error</body></html>
success.tpl
<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老是取不到資料,求大神解釋!