Home  >  Article  >  Backend Development  >  What databases does Go language support?

What databases does Go language support?

王林
王林Original
2024-03-01 13:18:041090browse

What databases does Go language support?

"What databases does the Go language support? 》

Go language is a simple and powerful programming language with rich database support. In the Go language, developers can use various types of databases, including relational databases, NoSQL databases, and in-memory databases. This article will introduce some common databases supported by the Go language and provide some specific code examples.

1. MySQL

MySQL is a common relational database that is widely used in web development. In the Go language, we can use third-party libraries to connect to the MySQL database and execute SQL queries. The following is a simple sample code:

package main

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

func main() {
    db, err := sql.Open("mysql", "username:password@tcp(127.0.0.1:3306)/database_name")
    if err != nil {
        panic(err.Error())
    }
    defer db.Close()

    // 查询数据
    rows, err := db.Query("SELECT * FROM users")
    if err != nil {
        panic(err.Error())
    }
    defer rows.Close()

    // 遍历结果集
    for rows.Next() {
        var id int
        var name string
        err = rows.Scan(&id, &name)
        if err != nil {
            panic(err.Error())
        }
        fmt.Printf("ID: %d, Name: %s
", id, name)
    }
}

2. MongoDB

MongoDB is a popular NoSQL database suitable for processing large amounts of unstructured data. In the Go language, you can use the officially provided MongoDB driver to connect and operate the MongoDB database. The following is a simple sample code:

package main

import (
    "context"
    "fmt"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
    clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
    client, err := mongo.Connect(context.Background(), clientOptions)
    if err != nil {
        panic(err.Error())
    }
    defer client.Disconnect(context.Background())

    collection := client.Database("test").Collection("users")

    // 插入数据
    _, err = collection.InsertOne(context.Background(), bson.D{
        {"name", "Alice"},
        {"age", 30},
    })
    if err != nil {
        panic(err.Error())
    }

    // 查询数据
    cursor, err := collection.Find(context.Background(), bson.D{})
    if err != nil {
        panic(err.Error())
    }
    defer cursor.Close(context.Background())

    for cursor.Next(context.Background()) {
        var result bson.M
        err := cursor.Decode(&result)
        if err != nil {
            panic(err.Error())
        }
        fmt.Println(result)
    }
}

The above is a simple sample code for connecting MySQL and MongoDB databases in Go language. In addition, Go language also supports databases such as Redis, SQLite, PostgreSQL, etc. Developers can choose the appropriate database for development according to their own needs. With these database supports, developers can easily build various types of applications to meet different business needs.

The above is the detailed content of What databases does Go language support?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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