Table of Contents
1. MySQL
2. PostgreSQL
3. MongoDB
Home Backend Development Golang What popular database systems are supported in Go language?

What popular database systems are supported in Go language?

Mar 28, 2024 am 08:36 AM
mysql mongodb go language

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

How to create a MySQL table using PHP?

The difference between performance testing and unit testing in Go language The difference between performance testing and unit testing in Go language May 08, 2024 pm 03:09 PM

The difference between performance testing and unit testing in Go language

See all articles