Comment développer une plateforme d'investissement en ligne simple en utilisant MySQL et le langage Go
Introduction :
En tant que service financier numérique, le développement de plateformes d'investissement en ligne attire de plus en plus d'attention. Cet article expliquera comment utiliser le langage MySQL et Go pour développer une plateforme d'investissement en ligne simple, y compris la conception de bases de données et des exemples de code associés.
import (
"database/sql" "fmt" "log" _ "github.com/go-sql-driver/mysql"
)
func connectDB() (*sql.DB, erreur) {
db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/database_name") if err != nil { log.Fatal(err) return nil, err } err = db.Ping() if err != nil { log.Fatal(err) return nil, err } fmt.Println("Connected to the database!") return db, nil
}
import (
"database/sql" "fmt" "log" "net/http"
)
func registerUser(w http.ResponseWriter, r *http.Request) {
username := r.FormValue("username") password := r.FormValue("password") email := r.FormValue("email") db, err := connectDB() if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } defer db.Close() stmt, err := db.Prepare("INSERT INTO User (Username, Password, Email) VALUES (?, ?, ?)") if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } defer stmt.Close() _, err = stmt.Exec(username, password, email) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } fmt.Fprintln(w, "Registration successful!")
}
import (
"database/sql" "fmt" "log" "net/http"
)
type Investment struct {
ID int Name string Amount float64 ExpectedReturn float64 InvestmentTerm int
}
func getInvestmentList(w http.ResponseWriter, r *http.Request) {
db, err := connectDB() if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } defer db.Close() rows, err := db.Query("SELECT * FROM Investment") if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } defer rows.Close() var investments []Investment for rows.Next() { var investment Investment err := rows.Scan(&investment.ID, &investment.Name, &investment.Amount, &investment.ExpectedReturn, &investment.InvestmentTerm) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } investments = append(investments, investment) } for _, investment := range investments { fmt.Fprintf(w, "ID: %d, Name: %s, Amount: %.2f, Expected Return: %.2f%%, Investment Term: %d months
", Investment.ID, Investment.Name, Investment.Amount, Investment.ExpectedReturn, Investment.InvestmentTerm)
}
}
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!