How golang implements MVC architecture

PHPz
Release: 2023-04-06 11:06:41
Original
996 people have browsed it

MVC (Model View Controller) is a commonly used software architecture pattern that divides an application into three core parts: model, view and controller. This separation can improve the maintainability, scalability, and reusability of the application. In Golang, we can easily implement MVC architecture.

1. Introduction to MVC

1.1 Model

The Model layer is the core part of the application. It is mainly responsible for processing data-related operations. The Model layer can contain the following content:

1.1.1 Database connection

The main job of the Model layer is to interact with the database. In Golang, we can use the officially provided database/sql library to connect to a relational database. For example:

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, err := sql.Open("mysql", "user:password@tcp(host:port)/dbname")
    // ...
}
Copy after login

1.1.2 Database operations

Through the database/sql library, we can perform various database operations. For example, query a table in the database:

rows, err := db.Query("SELECT * FROM users")
defer rows.Close()
for rows.Next() {
    var id int
    var username string
    var password string
    err := rows.Scan(&id, &username, &password)
    // ...
}
Copy after login

1.2 View

The View layer is responsible for displaying data, usually an HTML page. In Golang, we can use the HTML template engine to build the View layer. For example:

import (
    "html/template"
)

type User struct {
    Username string
    Password string
}

func main() {
    tpl := template.Must(template.ParseFiles("template.html"))
    user := User{
        Username: "user",
        Password: "password",
    }
    tpl.Execute(w, user)
}
Copy after login

1.3 Controller

The Controller layer is responsible for processing user requests and forwarding the requests to the Model layer and View layer. In Golang, HTTP router can be used to implement the functions of the Controller layer. For example:

import (
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    // 处理用户请求
    // ...
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}
Copy after login

2. Golang implements MVC

2.1 Model layer

In Golang, we can use struct to define the data structure of the Model layer, for example:

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

We can also define the methods of the Model layer through the interface, for example:

type UserRepository interface {
    FindByID(id int) (*User, error)
    FindAll() ([]*User, error)
    Store(user *User) error
    Delete(id int) error
}
Copy after login

Then, we can specifically implement the methods of the Model layer by implementing the UserRepository interface. For example:

type UserRepositoryImpl struct {
    db *sql.DB
}

func NewUserRepository(db *sql.DB) *UserRepositoryImpl {
    return &UserRepositoryImpl{
        db: db,
    }
}

func (r *UserRepositoryImpl) FindByID(id int) (*User, error) {
    var user *User
    err := r.db.QueryRow("SELECT * FROM users WHERE id = ?", id).
        Scan(&user.ID, &user.Username, &user.Password)
    if err != nil {
        return nil, err
    }
    return user, nil
}

func (r *UserRepositoryImpl) FindAll() ([]*User, error) {
    var users []*User
    rows, err := r.db.Query("SELECT * FROM users")
    if err != nil {
        return nil, err
    }
    for rows.Next() {
        var user User
        err := rows.Scan(&user.ID, &user.Username, &user.Password)
        if err != nil {
            return nil, err
        }
        users = append(users, &user)
    }
    return users, nil
}

func (r *UserRepositoryImpl) Store(user *User) error {
    stmt, err := r.db.Prepare("INSERT INTO users (username, password) VALUES (?, ?)")
    if err != nil {
        return err
    }
    _, err = stmt.Exec(user.Username, user.Password)
    if err != nil {
        return err
    }
    return nil
}

func (r *UserRepositoryImpl) Delete(id int) error {
    stmt, err := r.db.Prepare("DELETE FROM users WHERE id = ?")
    if err != nil {
        return err
    }
    _, err = stmt.Exec(id)
    if err != nil {
        return err
    }
    return nil
}
Copy after login

2.2 View layer

In Golang, we can use the HTML template engine to render the View layer, for example:

var tpl = `
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{{.Title}}</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Username</th>
                <th>Password</th>
            </tr>
        </thead>
        <tbody>
            {{range $index, $user := .Users}}
            <tr>
                <td>{{$user.ID}}</td>
                <td>{{$user.Username}}</td>
                <td>{{$user.Password}}</td>
            </tr>
            {{end}}
        </tbody>
    </table>
</body>
</html>
`

type PageData struct {
    Title string
    Users []*User
}

func handler(w http.ResponseWriter, r *http.Request) {
    userRepository := NewUserRepository(db)
    users, err := userRepository.FindAll()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    data := PageData{
        Title: "Users",
        Users: users,
    }
    tpl := template.Must(template.New("index").Parse(tpl))
    err = tpl.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}
Copy after login

2.3 Controller layer

In Golang, we can use HTTP router to handle user requests, for example:

func handler(w http.ResponseWriter, r *http.Request) {
    // 处理用户请求
    // ...
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}
Copy after login

We can also use the gorilla/mux library to implement advanced routing functions. For example:

func handler(w http.ResponseWriter, r *http.Request) {
    // 处理用户请求
    // ...
}

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", handler)
    r.HandleFunc("/users/{id}", getUser).Methods(http.MethodGet)
    http.ListenAndServe(":8080", r)
}

func getUser(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    id, _ := strconv.Atoi(vars["id"])
    userRepository := NewUserRepository(db)
    user, err := userRepository.FindByID(id)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    json.NewEncoder(w).Encode(user)
}
Copy after login

3. Conclusion

By implementing the MVC architecture in Golang, we can separate various parts of the application well, improving maintainability, scalability and complexity. Usability. In the implementation, we can use the officially provided database/sql library to operate the database, use the HTML template engine to render the page, and use the HTTP router to process user requests. At the same time, we can also use third-party libraries to implement advanced functions, such as the gorilla/mux library to implement advanced routing functions.

The above is the detailed content of How golang implements MVC architecture. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!