What popular database systems are supported in Go language?

WBOY
Release: 2024-03-28 08:36:04
Original
427 people have browsed it

What popular database systems are supported in Go language?

Title: Popular database systems and examples supported in Go language

Go language is an efficient and concise development language, and its support for databases is also very extensive. of. In the Go language, developers can easily operate a variety of popular database systems, including MySQL, PostgreSQL, MongoDB, etc. This article will introduce several popular database systems supported in the Go language and give corresponding code examples for each database.

1. MySQL

MySQL is a commonly used relational database system. Go language can connect and operate MySQL through third-party libraries. The following is a simple sample code that demonstrates how to use Go language to connect to a MySQL database and query data:

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.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.Println(id, name)
    }
}
Copy after login

2. PostgreSQL

PostgreSQL is an open source relational database system, Go The language also provides support for PostgreSQL. The following is a simple sample code that demonstrates how to use Go language to connect to a PostgreSQL database and insert data:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/lib/pq"
)

func main() {
    db, err := sql.Open("postgres", "user=username password=password dbname=dbname sslmode=disable")
    if err != nil {
        panic(err.Error())
    }
    defer db.Close()

    _, err = db.Exec("INSERT INTO users (name) VALUES ('Alice')")
    if err != nil {
        panic(err.Error())
    }

    fmt.Println("Data inserted successfully")
}
Copy after login

3. MongoDB

MongoDB is a non-relational database system, Go language MongoDB connection and operation can be performed through third-party libraries. The following is a simple sample code that demonstrates how to use Go language to connect to a MongoDB database and insert data:

package main

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

type User struct {
    Name string
    Age  int
}

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("mydb").Collection("users")

    user := User{Name: "Bob", Age: 30}
    _, err = collection.InsertOne(context.Background(), user)
    if err != nil {
        panic(err.Error())
    }

    fmt.Println("Data inserted successfully")
}
Copy after login

The above are several popular database systems supported in Go language and corresponding code examples. Developers can choose the appropriate database system according to their own needs and easily perform database operations through the Go language.

The above is the detailed content of What popular database systems are supported in Go language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!