How to use Go language to write the user account management module in the door-to-door cooking system?
In modern fast-paced life, more and more people choose to solve their dietary problems through door-to-door cooking services. For this service provider, a complete and sound user account management system is very important. This article will introduce how to use Go language to write a user account management module, aiming to help home cooking service providers better manage users' account information, and provide specific code examples.
1. Create a database table
First, you need to create a user account table to store the user's account information. Data can be stored using relational databases (such as MySQL, PostgreSQL, etc.) or non-relational databases (such as MongoDB).
In MySQL, you can use the following SQL statement to create a user account table:
CREATE TABLE user ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL );
2. Write Go code
Next, we use Go language to write a user Code for the account management module. First, you need to introduce the necessary packages:
package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" )
Then, define a structure to represent the user account:
type User struct { ID int Username string Password string Email string }
Then, write some functions to implement specific functions:
func connectDB() (*sql.DB, error) { db, err := sql.Open("mysql", "username:password@tcp(host:port)/database") if err != nil { return nil, err } return db, nil }
func createUser(user User) error { db, err := connectDB() if err != nil { return err } defer db.Close() _, err = db.Exec("INSERT INTO user (username, password, email) VALUES (?, ?, ?)", user.Username, user.Password, user.Email) if err != nil { return err } return nil }
func getUserByUsername(username string) (User, error) { db, err := connectDB() if err != nil { return User{}, err } defer db.Close() var user User err = db.QueryRow("SELECT id, username, password, email FROM user WHERE username = ?", username). Scan(&user.ID, &user.Username, &user.Password, &user.Email) if err != nil { return User{}, err } return user, nil }
func updateUser(user User) error { db, err := connectDB() if err != nil { return err } defer db.Close() _, err = db.Exec("UPDATE user SET username = ?, password = ?, email = ? WHERE id = ?", user.Username, user.Password, user.Email, user.ID) if err != nil { return err } return nil }
func deleteUser(id int) error { db, err := connectDB() if err != nil { return err } defer db.Close() _, err = db.Exec("DELETE FROM user WHERE id = ?", id) if err != nil { return err } return nil }
3. Test code
Finally, you can write some test code to verify the correctness of these functions:
func main() { // 创建用户账户 user := User{ Username: "john", Password: "123456", Email: "john@example.com", } err := createUser(user) if err != nil { fmt.Println(err) return } // 根据用户名查询用户账户 retrievedUser, err := getUserByUsername("john") if err != nil { fmt.Println(err) return } fmt.Println(retrievedUser) // 更新用户账户 retrievedUser.Username = "john2" err = updateUser(retrievedUser) if err != nil { fmt.Println(err) return } // 删除用户账户 err = deleteUser(retrievedUser.ID) if err != nil { fmt.Println(err) return } }
The above are the detailed steps and specific code examples for using Go language to write the user account management module in the door-to-door cooking system. By implementing this module, home cooking service providers can better manage users' account information and provide a better user experience. Hope this article helps you!
The above is the detailed content of How to use Go language to write the user account management module in the door-to-door cooking system?. For more information, please follow other related articles on the PHP Chinese website!