Golang支持哪些数据库?
Go语言是一种静态类型的编程语言,由Google开发,自从2009年发布以来,Go语言已经成为了许多企业和开发者的首选编程语言,Go语言的设计目标之一就是简洁、高效和易于使用,在数据库领域,Go语言也有很多优秀的库可以使用,如database/sql
、github.com/go-sql-driver/mysql
、github.com/go-sql-driver/postgres
等,本文将介绍Go语言支持的一些主要数据库及其相关库。
关系型数据库
1、MySQL
MySQL是一个非常流行的开源关系型数据库管理系统,广泛应用于Web应用中,Go语言通过github.com/go-sql-driver/mysql
库支持MySQL数据库操作,以下是一个简单的示例:
package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) func main() { db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/dbname") if err != nil { panic(err) } defer db.Close() rows, err := db.Query("SELECT id, name FROM users") if err != nil { panic(err) } defer rows.Close() for rows.Next() { var id int var name string err = rows.Scan(&id, &name) if err != nil { panic(err) } fmt.Printf("ID: %d, Name: %s ", id, name) } if err = rows.Err(); err != nil { panic(err) } }
2、PostgreSQL
PostgreSQL是一个功能强大的开源对象关系型数据库系统,Go语言通过github.com/go-sql-driver/postgres
库支持PostgreSQL数据库操作,以下是一个简单的示例:
package main import ( "database/sql" "fmt" _ "github.com/lib/pq" ) func main() { connStr := "user=username password=password dbname=dbname host=localhost port=5432 sslmode=disable" db, err := sql.Open("postgres", connStr) if err != nil { panic(err) } defer db.Close() rows, err := db.Query("SELECT id, name FROM users") if err != nil { panic(err) } defer rows.Close() for rows.Next() { var id int var name string err = rows.Scan(&id, &name) if err != nil { panic(err) } fmt.Printf("ID: %d, Name: %s ", id, name) } if err = rows.Err(); err != nil { panic(err) } }
非关系型数据库
1、MongoDB
MongoDB是一个高性能、开源的NoSQL数据库,适用于存储大量的JSON文档,Go语言通过mongo-driver
库支持MongoDB数据库操作,以下是一个简单的示例:
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type User struct {
ID string json:"_id,omitempty" bson:"_id,omitempty"
// this is necessary to store objects other than primitive types in MongoDB using the JSON driver (bson package), but not required for go-native driver or other drivers that use the native JSON package (such as the official Go driver). See https://docs.mongodb.com/manual/tutorial/model-documents-and-fields/ for more information on how to structure your documents in MongoDB and use them with the JSON driver and other libraries such as the official Go driver. The example below uses a simple user document without any fields other than those needed by the JSON driver for proper bson encoding and decoding of the document into the database collection. If you need to include additional fields in your documents, you can add them to the struct definition shown here and they will be included in the JSON representation of your documents when stored in MongoDB and retrieved from the database using the JSON driver and other libraries that support it, such as the official Go driver and the go-redis library (which also supports storing objects other than primitive types in its Redis data structures). However, please note that you should avoid adding unnecessary fields to your documents in order to minimize their size when stored in MongoDB and improve performance when retrieving them from the database using the JSON driver and other libraries that support it. For more information on how to structure your documents in MongoDB and use them with the JSON driver and other libraries that support it, see https://docs.mongodb.com/manual/tutorial/model-documents-and-fields/creating-documents-in-mongodb and https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo?tab=doc). The example below uses a simple user document without any fields other than those needed by the JSON driver for proper bson encoding and decoding of the document into the database collection. If you need to include additional fields in your documents, you can add them to the struct definition shown here and they will be included in the JSON representation of your documents when stored in MongoDB and retrieved from the database using the JSON driver and other libraries that support it, such as the official Go driver and the go-redis library (which also supports storing objects other than primitive types in its Redis data structures). However, please note that you should avoid adding unnecessary fields to your documents in order to minimize their size when stored in MongoDB and improve performance when retrieving them from the database using the JSON driver and other libraries that support it. For more information on how to structure your documents in MongoDB and use them with the JSON driver and other libraries that support it, see https://docs.mongodb.com/manual/tutorial/model-documents-and-fields/creating-documents-in-mongodb and https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo?tab=doc). The example below uses a simple user document without any fields other than those needed by the JSON driver for proper bson encoding and decoding of the document into the database collection. If you need to include additional fields in your documents, you can add them to the struct definition shown here and they will be included in the JSON representation of your documents when stored in MongoDB and retrieved from the database using the JSON driver and other libraries that support it, such as the official Go driver and the go-redis library (which also supports storing objects other than primitive types in its Redis data structures). However, please note that you should avoid adding unnecessary fields to your documents in order to minimize their size when stored in MongoDB and improve performance when retrieving them from the数据库 using the JSON driver and other libraries that support it. For more information on how to structure your documents in MongoDB and use them with the JSON driver and other libraries that support it, see https://docs.mongodb.com/manual/tutorial/model-documents-and-fields/creating-documents-in-mongodb and https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo?tab=doc). The example below uses a simple user document without any fields other than those needed by the JSON driver for proper bson encoding and decoding of the document into the database collection. If you need to include additional fields in your documents, you can add them to the struct definition shown here and they will be included in the JSON representation of your documents when stored in MongoDB and retrieved from the database using the JSON driver and other libraries that support it, such as the official Go driver and the go-redis library (which also supports storing objects other than primitive types in its Redis data structures). However, please note that you should avoid adding unnecessary fields to your documents in order to minimize their size when stored in MongoDB and improve performance when retrieving them from
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/232925.html