go語言實現一個簡單的登入註冊web小程式

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

    最近學習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老是取不到資料,求大神解釋!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.