最近学习golang也有一段时间了,基础差不多学了个大概,因为本人是java程序员,所以对web更感兴趣。根据《go web编程》中的例子改编一个更简单的例子,供新手参考,废话不多说,上菜:
这个例子使用到了beego框架和beedb框架,如果是go新手beego和beedb得自己去google下载安装。
目录结构:
index.go
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.go
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.go
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
<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老是取不到数据,求大神解释!