Exploration of commonly used database selections in Go language
引言:
在现代的软件开发中,无论是Web应用、移动应用还是物联网应用,都离不开数据的存储和查询。而在Go语言中,我们有许多优秀的数据库选择。本文将Exploration of commonly used database selections in Go language,并提供具体的代码示例,帮助读者了解和选择适合自己需求的数据库。
一、SQL数据库
例如,以下是一个使用MySQL数据库的示例代码:
package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) func main() { db, err := sql.Open("mysql", "root:password@tcp(localhost:3306)/test") if err != nil { fmt.Println("Failed to connect to MySQL:", err) return } defer db.Close() // 查询数据 rows, err := db.Query("SELECT * FROM users") if err != nil { fmt.Println("Failed to execute query:", err) return } defer rows.Close() for rows.Next() { var id int var name string err := rows.Scan(&id, &name) if err != nil { fmt.Println("Failed to scan row:", err) return } fmt.Println("ID:", id, "Name:", name) } if err := rows.Err(); err != nil { fmt.Println("Failed to retrieve data:", err) return } // 插入数据 result, err := db.Exec("INSERT INTO users (name) VALUES (?)", "John") if err != nil { fmt.Println("Failed to insert data:", err) return } fmt.Println("Insert ID:", result.LastInsertId()) }
以下是一个使用PostgreSQL数据库的示例代码:
package main import ( "database/sql" "fmt" _ "github.com/lib/pq" ) func main() { db, err := sql.Open("postgres", "user=postgres password=password dbname=mydb sslmode=disable") if err != nil { fmt.Println("Failed to connect to PostgreSQL:", err) return } defer db.Close() // 查询数据 rows, err := db.Query("SELECT * FROM users") if err != nil { fmt.Println("Failed to execute query:", err) return } defer rows.Close() for rows.Next() { var id int var name string err := rows.Scan(&id, &name) if err != nil { fmt.Println("Failed to scan row:", err) return } fmt.Println("ID:", id, "Name:", name) } if err := rows.Err(); err != nil { fmt.Println("Failed to retrieve data:", err) return } // 插入数据 result, err := db.Exec("INSERT INTO users (name) VALUES ($1)", "John") if err != nil { fmt.Println("Failed to insert data:", err) return } fmt.Println("Insert ID:", result.LastInsertId()) }
二、NoSQL数据库
以下是一个使用MongoDB数据库的示例代码:
package main import ( "context" "fmt" "log" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type User struct { ID string Name string } func main() { client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017")) if err != nil { log.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() err = client.Connect(ctx) if err != nil { log.Fatal(err) } defer client.Disconnect(ctx) collection := client.Database("test").Collection("users") // 查询数据 cur, err := collection.Find(ctx, bson.D{}) if err != nil { log.Fatal(err) } defer cur.Close(ctx) for cur.Next(ctx) { var user User if err := cur.Decode(&user); err != nil { log.Fatal(err) } fmt.Println("ID:", user.ID, "Name:", user.Name) } if err := cur.Err(); err != nil { log.Fatal(err) } // 插入数据 user := User{ID: "1", Name: "John"} _, err = collection.InsertOne(ctx, user) if err != nil { log.Fatal(err) } fmt.Println("Insert ID:", user.ID) }
总结:
本文探索了Go语言中常用的数据库选择,包括SQL数据库(如MySQL和PostgreSQL)和NoSQL数据库(如MongoDB)。通过具体的代码示例,读者可以了解到如何使用这些数据库,并根据自己的需求选择适合的数据库。当然,这些数据库仅仅是众多选择中的一部分,读者也可以根据具体的项目需求选择其他数据库。
The above is the detailed content of Exploration of commonly used database selections in Go language. For more information, please follow other related articles on the PHP Chinese website!