Home  >  Article  >  Backend Development  >  How does the golang framework interact with NoSQL databases?

How does the golang framework interact with NoSQL databases?

WBOY
WBOYOriginal
2024-06-04 12:35:58240browse

The Go framework provides powerful capabilities for interacting with NoSQL databases through the standard library and third-party frameworks. These frameworks include: 1. MongoDB library: mgo; 2. Redis library: redigo; 3. Cassandra library: gocql. These frameworks provide APIs for interacting with MongoDB, Redis, and Cassandra databases, including operations such as establishing connections, executing queries, inserting, updating, and deleting documents.

How does the golang framework interact with NoSQL databases?

Go framework interacts with NoSQL databases

NoSQL databases are popular for their scalability, flexibility and large storage capabilities Increasingly popular in modern applications. The Go language provides powerful capabilities for interacting with NoSQL databases through its standard library and rich third-party frameworks.

Here's how to use the Go framework to interact with NoSQL databases:

1. MongoDB

  • Library: [ mgo](https://godoc.org/gopkg.in/mgo.v2)
package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "gopkg.in/mgo.v2"
)

func main() {
    // 建立连接
    session, err := mgo.Dial("mongodb://localhost:27017")
    if err != nil {
        log.Fatal(err)
    }
    defer session.Close()

    // 获取所选数据库和集合
    db := session.DB("test")
    collection := db.C("users")

    // 插入文档
    if err := collection.Insert(map[string]interface{}{"name": "Alice", "age": 30}); err != nil {
        log.Fatal(err)
    }

    // 查找文档
    result := &struct{ Name string }{Name: ""}
    if err := collection.Find(nil).One(result); err != nil {
        log.Fatal(err)
    }
    fmt.Println("Name:", result.Name)

    // 更新文档
    if _, err := collection.Update(map[string]interface{}{"name": "Alice"}, map[string]interface{}{"age": 31}); err != nil {
        log.Fatal(err)
    }

    // 删除文档
    if err := collection.Remove(map[string]interface{}{"name": "Alice"}); err != nil {
        log.Fatal(err)
    }

}

2. Redis

  • Library: [redigo](https://godoc.org/github.com/garyburd/redigo/redis)
package main

import (
    "fmt"
    "log"

    "github.com/garyburd/redigo/redis"
)

func main() {
    // 建立连接
    conn, err := redis.Dial("tcp", "localhost:6379")
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()

    // 设置键值对
    if _, err := conn.Do("SET", "name", "Alice"); err != nil {
        log.Fatal(err)
    }

    // 获取值
    name, err := redis.String(conn.Do("GET", "name"))
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Name:", name)

    // 删除键
    if _, err := conn.Do("DEL", "name"); err != nil {
        log.Fatal(err)
    }
}

3. Cassandra

  • Library: [gocql](https://godoc.org/github.com/gocql/gocql)
package main

import (
    "fmt"
    "log"
    "time"

    "github.com/gocql/gocql"
)

func main() {
    // 建立连接
    cluster := gocql.NewCluster("localhost:9042")
    session, err := cluster.CreateSession()
    if err != nil {
        log.Fatal(err)
    }
    defer session.Close()

    // 创建表
    if err := session.Query(`CREATE TABLE users (name text, age int, PRIMARY KEY (name))`).Exec(); err != nil {
        log.Fatal(err)
    }

    // 插入数据
    if err := session.Query(`INSERT INTO users (name, age) VALUES (?, ?)`).Bind("Alice", 30).Exec(); err != nil {
        log.Fatal(err)
    }

    // 查询数据
    iter := session.Query(`SELECT * FROM users`).Iter()
    var name string
    var age int
    for iter.Scan(&name, &age) {
        fmt.Println("Name:", name, "Age:", age)
    }
    if err := iter.Close(); err != nil {
        log.Fatal(err)
    }

    // 更新数据
    if err := session.Query(`UPDATE users SET age = ? WHERE name = ?`).Bind(31, "Alice").Exec(); err != nil {
        log.Fatal(err)
    }

    // 删除数据
    if err := session.Query(`DELETE FROM users WHERE name = ?`).Bind("Alice").Exec(); err != nil {
        log.Fatal(err)
    }
}

The above is the detailed content of How does the golang framework interact with NoSQL databases?. 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