Home > Backend Development > Golang > How to use Go language to write the user account management module in the door-to-door cooking system?

How to use Go language to write the user account management module in the door-to-door cooking system?

WBOY
Release: 2023-11-01 14:10:50
Original
884 people have browsed it

How to use Go language to write the user account management module in the door-to-door cooking system?

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
);
Copy after login

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"
)
Copy after login

Then, define a structure to represent the user account:

type User struct {
    ID       int
    Username string
    Password string
    Email    string
}
Copy after login

Then, write some functions to implement specific functions:

  1. Connect database function:
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
}
Copy after login
  1. Create user account function:
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
}
Copy after login
  1. Query user account function based on user name:
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
}
Copy after login
  1. Update user account function:
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
}
Copy after login
  1. Delete user account function:
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
}
Copy after login

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
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template