
The method for golang to implement user login registration is as follows:
The first step is to register models
Create models.go under models
models.go File
package models
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
func RegisterDB() {
//注册驱动
orm.RegisterDriver("mysql", orm.DRMySQL)
//数据库链接
//注册默认数据库
var db_url string = beego.AppConfig.String("username_DB") + ":" + beego.AppConfig.String("password_DB") + "@tcp(" + beego.AppConfig.String("host_DB") + ")/" + beego.AppConfig.String("name_DB") + "?charset=" + beego.AppConfig.String("charset")
beego.Info(db_url)
orm.RegisterDataBase("default", "mysql", db_url)
// orm.RegisterDataBase("default", "mysql", "an:111@tcp(127.0.0.1:3306)/yoo_home?charset=utf8")
// //注册model
orm.RegisterModel(new(TUser))
}The second step requires database connection
The app.conf file under conf
appname = an httpport = 8080 runmode = dev sessionon = true #数据库为mysql host_DB = "127.0.0.1" port_DB = "3306" charset = "utf8" name_DB = "ancg" username_DB = "an" password_DB = 111
The third step writes a simple front-end view interface
## Create the client.html file under #views<!DOCTYPE html> <html> <head> <title>客户端接口测试</title> </head> <body> <label>注册</label> <form action="/client " method="POST"> <label>[options == register 注册]</label> <div>options:<input type="text" value="register" name="options"></div> <div>tel:<input type="text" name="Tel"></div> <div>pwd:<input type="text" name="Pwd"></div> <input type="submit" name="注册"Submit/> </form> <br> <label>登录</label> <form action="/client " method="POST"> <label>[options == login 登录]</label> <div>options:<input type="text" value="login" name="options"></div> <div>tel:<input type="text" name="Tel"></div> <div>pwd:<input type="text" name="Pwd"></div> <input type="submit" name="注册"Submit/> </form> </body> </html>The fourth step is to create TUser in models to automatically create tables for the database.TUser.go
package models
import (
"github.com/astaxie/beego/orm"
//_"github.com/go-sql-driver/mysql"
)
//用户表
type TUser struct {
//用户序号
Id int64
//电话号码
Tep string
//密码
Pwd string
//收款人
Payee string
//地址
Address string
//收款帐号
Amount string
//账号类别
AmountType string
//是否消费者
IsCustomer bool
//是否商家
IsSeller bool
//是否配送员
IsDiliver bool
//是否管理员
IsManager bool
//微信openId
Vid string
//是否冻结
IsLock bool
//创建时间 --- 时间戳
AddTime int64
}
//新建用户
func AddUser(user *TUser) (int64, error) {
o := orm.NewOrm() //数据库
userId, err := o.Insert(user) //插入数据
return userId, err
}
//查询账号
func GetUserById(userId int64) (*TUser, error) {
o := orm.NewOrm() //数据库
user := new(TUser) //TUser就是第9行struct的数据库,就是mysql的表
qs := o.QueryTable("t_user") //表名为t_user
err := qs.Filter("id", userId).One(user) //One是指查询一条数据,One(user)是查询mysql表中一条数据
return user, err
}
//手机号查询账号
func GetUserByTel(tel string) (*TUser, error) {
o := orm.NewOrm()
user := new(TUser) //TUser就是第9行struct的数据库,就是mysql的表
qs := o.QueryTable("t_user") //表名为t_user
err := qs.Filter("tel", tel).One(user) //One是指查询一条数据,One(user)是查询mysql表中一条数据
return user, err
}
//微信Id查询账号
func GetUserByVid(vid int64) (*TUser, error) {
o := orm.NewOrm()
user := new(TUser) //TUser就是第9行struct的数据库,就是mysql的表
qs := o.QueryTable("t_user") //表名为t_user
err := qs.Filter("vid", vid).One(user) //One是指查询一条数据,One(user)是查询mysql表中一条数据
return user, err
}The fifth step Create a file in controllers that connects to options, and use the corresponding options to call other controllersclient.go filepackage controllers
import (
"github.com/astaxie/beego"
"time"
)
type ClientController struct {
beego.Controller
}
func (this *ClientController) Get() {
this.TplName = "client.html"
}
func (this *ClientController) Post() {
options := this.Input().Get("options")
beego.Info(options)
//请求检查方法
if options != "" {
switch options {
case "login":
this.login()
case "register":
this.register()
default:
this.Data["json"] = map[string]interface{}{"status": 400, "msg": "无对应处理方法!", "time": time.Now().Format("2006-12-12 12:12:12")}
this.ServeJSON()
return
}
this.Data["json"] = map[string]interface{}{"status": 400, "msg": "options为空", "time": time.Now().Format("2006-12-12 12:12:12")}
this.ServeJSON()
return
}
}For more golang knowledge, please pay attention to the golang tutorial column .
The above is the detailed content of How to log in as golang user. For more information, please follow other related articles on the PHP Chinese website!
The Performance Race: Golang vs. CApr 16, 2025 am 12:07 AMGolang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.
Golang vs. C : Code Examples and Performance AnalysisApr 15, 2025 am 12:03 AMGolang is suitable for rapid development and concurrent programming, while C is more suitable for projects that require extreme performance and underlying control. 1) Golang's concurrency model simplifies concurrency programming through goroutine and channel. 2) C's template programming provides generic code and performance optimization. 3) Golang's garbage collection is convenient but may affect performance. C's memory management is complex but the control is fine.
Golang's Impact: Speed, Efficiency, and SimplicityApr 14, 2025 am 12:11 AMGoimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:
C and Golang: When Performance is CrucialApr 13, 2025 am 12:11 AMC is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.
Golang in Action: Real-World Examples and ApplicationsApr 12, 2025 am 12:11 AMGolang excels in practical applications and is known for its simplicity, efficiency and concurrency. 1) Concurrent programming is implemented through Goroutines and Channels, 2) Flexible code is written using interfaces and polymorphisms, 3) Simplify network programming with net/http packages, 4) Build efficient concurrent crawlers, 5) Debugging and optimizing through tools and best practices.
Golang: The Go Programming Language ExplainedApr 10, 2025 am 11:18 AMThe core features of Go include garbage collection, static linking and concurrency support. 1. The concurrency model of Go language realizes efficient concurrent programming through goroutine and channel. 2. Interfaces and polymorphisms are implemented through interface methods, so that different types can be processed in a unified manner. 3. The basic usage demonstrates the efficiency of function definition and call. 4. In advanced usage, slices provide powerful functions of dynamic resizing. 5. Common errors such as race conditions can be detected and resolved through getest-race. 6. Performance optimization Reuse objects through sync.Pool to reduce garbage collection pressure.
Golang's Purpose: Building Efficient and Scalable SystemsApr 09, 2025 pm 05:17 PMGo language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.
Why do the results of ORDER BY statements in SQL sorting sometimes seem random?Apr 02, 2025 pm 05:24 PMConfused about the sorting of SQL query results. In the process of learning SQL, you often encounter some confusing problems. Recently, the author is reading "MICK-SQL Basics"...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use






