The go language implements a simple login registration Web Applet

Source: Internet
Author: User

Recently studied Golang also has a period of time, the foundation almost learned a probably, because I am a Java programmer, so more interested in the Web. According to the example of Go Web programming, a simpler example is adapted for beginners ' reference, and no more nonsense to say, serving:

This example uses the Beego framework and the BEEDB framework, if the go novice beego and beedb have to go to Google to download the installation.

Directory structure:

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:[email Protected] (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 ("User name or password is wrong!") ")}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



Success.tpl



Index.tpl



Regist.tpl

MySQL is used in the database, the table statement is built

CREATE TABLE User (id      int,username varchar (+)   , pwd varchar (+), PRIMARY KEY (id));

The program is not a problem to run, but the session is always get no data, ask God to explain!

The go language implements a simple login registration Web Applet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.