Sobes.tech
Junior — Middle

What tools or libraries have you used to implement database interaction in your projects?

sobes.tech AI

Answer from AI

In Go projects, the standard database/sql package is often used for interacting with databases, providing a common interface for working with different DBMSs. Specific drivers like github.com/lib/pq for PostgreSQL or github.com/go-sql-driver/mysql for MySQL are used.

Additionally, popular ORM libraries and tools simplify database operations:

  • GORM — a powerful ORM with support for migrations, associations, and a user-friendly API.
  • sqlx — an extension of database/sql with convenient methods for scanning and working with queries.

Example of using database/sql with PostgreSQL:

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

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

    var name string
    err = db.QueryRow("SELECT name FROM users WHERE id=$1", 1).Scan(&name)
    if err != nil {
        panic(err)
    }
    fmt.Println("User name:", name)
}
What tools or libraries have you used to implement… - sobes.tech