Golang Study of Beego and additions and changes to the code implementation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Record the use of Beego to delete and change the implementation of the database using MySQL, the complete code is as follows:

 PackageMainImport ( _ "Crud_beego/routers"//Auto-Enrollment Routing"FMT" "Github.com/astaxie/beego" "Github.com/astaxie/beego/orm" _ "Github.com/go-sql-driver/mysql" ) Const(driver_name= "MySQL"Data_source= "Root:123456@tcp (127.0.0.1:3306)/test?charset=utf8&loc=asia%2fshanghai"Max_idle_conn= 5Max_open_conn= 30) type User struct {IdintName String Email string ageintisActive BOOL} func (U*User) TableName () string {return"T_user"} func Registerdb () {orm. Debug=trueORM. RegisterDatabase ("Default", Driver_name, Data_source, Max_idle_conn, Max_open_conn) Orm. Registermodel (New(User))} Func ( ThisUser) ToString () string {returnFmt. Sprintf ("Id:%d\tname:%s\temail:%s\tage:%d\tisactive:%v", This. Id This. Name, This. Email, This. Age, This. IsActive)} Func createusers () {users:=[]user{user{name:"Adadmin1", Email: "14199@111.com", Age:12}, User{name:"Adadmin1", Email: "14199@111.com", Age:12}, User{name:"Adadmin1", Email: "14199@111.com", Age:12}, User{name:"Adadmin1", Email: "14199@111.com", Age:12}, User{name:"Adadmin1", Email: "14199@111.com", Age:12}, User{name:"Adadmin1", Email: "14199@111.com", Age:12}, User{name:"Adadmin1", Email: "14199@111.com", Age:12},} o:=ORM. Neworm ()ifSuccessnums, err: = O.insertmulti (len (Users), users); Err! =Nil {fmt. Println ("Insert Fail ...") } Else{fmt. Println ("Success inserted%d Datas", Successnums)} } //Find All Usersfunc listUsers () {var users []user Orm. Neworm (). QueryTable ("T_user"). All (&users) for_, User: =range Users {fmt. Println (user. ToString ())}}//Get User Countfunc Countuser () {cnt, _:= orm. Neworm (). QueryTable ("T_user"). Count () fmt. Println ("All User count:", CNT)} //get the only one userfunc GetUser () {var user user err:= orm. Neworm (). QueryTable ("T_user"). Filter ("Id", 5). One (&user)ifErr = =Nil {fmt. Println (user. ToString ())}}//get the only one userfunc getusers () {var users []user _, Err:= orm. Neworm (). QueryTable ("T_user"). Filter ("Name__contains", "AWD"). All (&users)ifErr = =Nil { for_, User: =range Users {fmt. Println (user. ToString ()) }}}//Get limit userfunc Limituser () {var users []user _, Err:= orm. Neworm (). QueryTable ("T_user"). Limit (6). ("-name"). All (&users)ifErr = =Nil { for_, User: =range Users {fmt. Println (user. ToString ()) }}}//Get user Limit,offsetfunc Limitoffsetuser () {var users []user _, Err:= orm. Neworm (). QueryTable ("T_user"). Limit (1, 4). ("Id"). All (&users)ifErr = =Nil { for_, User: =range Users {fmt. Println (user. ToString ()) }}}//del userfunc deluser () {num, err:= orm. Neworm (). QueryTable ("T_user"). Filter ("Id", "5"). Delete () fmt. Printf ("Affected Num:%s,%s", num, err)} //Update Userfunc updateUser () {num, err:= orm. Neworm (). QueryTable ("T_user"). Filter ("Name__contains", "AWD"). Update (ORM. params{"Name": "#########", }) Fmt. Printf ("Affected Num:%s,%s", num, err)} Func Main () {Registerdb () createusers () listUsers () Countuser () GetUser () getusers () Limitoffsetuser () Deluser () updateu Ser () Beego. Run ()}

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.