This is a creation in Article, where the information may have evolved or changed.
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: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 ("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));
Program is no problem to run, but the session is always get no data, ask God to explain!