#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Databases

Working with Databases in Go – GORM and SQL Connections

Using database/sql Directly

db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/dbname")
defer db.Close()

rows, err := db.Query("SELECT id, name FROM users")
for rows.Next() {
    var id int
    var name string
    rows.Scan(&id, &name)
    fmt.Println(id, name)
}

Using GORM (ORM Library)

GORM simplifies database interactions with struct-based models instead of raw SQL.

type User struct {
    ID   uint
    Name string
}

db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})

db.Create(&User{Name: "Alice"})

var users []User
db.Find(&users)

Choosing Between Them

database/sql gives full control and is dependency-free, while GORM speeds up development with automatic migrations, associations, and query building at the cost of some added abstraction.

Ready to master real-world Go skills?

Learn Go hands-on with mentor-led, live sessions.

Explore Course