go 语言编写的简易框架
立即下载
资源介绍:
简易的Go语言开发框架可以帮助你快速启动一个项目,并且提供基础的功能结构。以下是一个基础的Go语言开发框架的结构,包含一些常见的组件如路由、配置、日志和数据库连接首先,定义你的项目的目录结构:my-go-project/
│
├── cmd/
│ └── main.go # 主程序入口
├── config/
│ └── config.go # 配置文件加载
├── internal/
│ ├── api/
│ │ └── handler.go # API 路由和处理
│ ├── models/
│ │ └── user.go # 数据模型
│ ├── service/
│ │ └── user_service.go # 业务逻辑层
│ └── repository/
│ └── user_repo.go # 数据库操作层
├── dao/
│ ├── database/
│ │ └── database.go # 数据库连接
│ ├──
package service
import (
"database/sql"
"encoding/json"
"go_pay/dao/database"
"go_pay/entity"
"log"
"net/http"
)
var db *sql.DB
// Initialize 设置数据库连接
func Initialize() {
db = database.GetDB()
if db == nil {
log.Fatal("Database not initialized")
}
}
// handleError 是一个公共的错误处理函数
func handleError(w http.ResponseWriter, statusCode int, message string, err error) {
http.Error(w, message, statusCode)
if err != nil {
log.Printf("%s: %v", message, err)
}
}
// GetUsers retrieves a list of users from the database
func GetUsers(w http.ResponseWriter, r *http.Request) {
if db == nil {
http.Error(w, "Database not initialized", http.StatusInternalServerError)
return
}
users := []entity.User{}
rows, err := db.Query("SELECT id, name, email FROM user")
if err != nil {
http.Error(w, "Failed to retrieve users", http.StatusInternalServerError)
log.Printf("Failed to retrieve users: %v", err)
return
}
defer rows.Close()
for rows.Next() {
var user entity.User
if err := rows.Scan(&user.ID, &user.Name, &user.Email); err != nil {
http.Error(w, "Failed to scan user data", http.StatusInternalServerError)
log.Printf("Failed to scan user data: %v", err)
return
}
users = append(users, user)
}
if err = rows.Err(); err != nil {
http.Error(w, "Error occurred during row iteration", http.StatusInternalServerError)
log.Printf("Error occurred during row iteration: %v", err)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(users); err != nil {
http.Error(w, "Failed to encode users data", http.StatusInternalServerError)
log.Printf("Failed to encode users data: %v", err)
}
}
// CreateUser 插入新用户并返回创建的用户数据
func CreateUser(w http.ResponseWriter, r *http.Request) {
if db == nil {
handleError(w, http.StatusInternalServerError, "Database not initialized", nil)
return
}
var user entity.User
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
handleError(w, http.StatusBadRequest, "Invalid request payload", err)
return
}
query := "INSERT INTO user (name, email) VALUES (?, ?)"
result, err := db.Exec(query, user.Name, user.Email)
if err != nil {
handleError(w, http.StatusInternalServerError, "Failed to create user", err)
return
}
id, err := result.LastInsertId()
if err != nil {
handleError(w, http.StatusInternalServerError, "Failed to retrieve last insert ID", err)
return
}
user.ID = int(id)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(user); err != nil {
handleError(w, http.StatusInternalServerError, "Failed to encode user data", err)
}
}
资源文件列表:
go_pay.zip 大约有191个文件