Hello Kiran,
The standard package doesn’t provide you advanced features of ORM or a driver library. You can use bulk updates provided by your driver library.
I don’t know which database you are using at the moment but I am assuming it is postgreSQL. With a driver library, you can easily do your bulk updates.
I have also found the raw SQL to generate a bulk update so you can use it within database/sql if you do not wish to use a driver library
Additional note: db.prepare doesn’t make you have bulk insert out of the box. It’s sole purpose is to have a transaction ongoing for your database.
See the example:
https://pkg.go.dev/github.com/go-pg/pg#example-DB-Update-BulkUpdate
db := modelDB()
book1 := &Book{
Id: 1,
Title: "updated book 1",
UpdatedAt: time.Now(),
}
book2 := &Book{
Id: 2,
Title: "updated book 2",
UpdatedAt: time.Now(),
}
// UPDATE "books" AS "book"
// SET "title" = _data."title"
// FROM (VALUES ('updated book 1', 1), ('updated book 2', 2)) AS _data("title", "id")
// WHERE "book"."id" = _data."id"
_, err := db.Model(book1, book2).Column("title", "updated_at").Update()
if err != nil {
panic(err)
}
var books []Book
err = db.Model(&books).Order("id").Select()
if err != nil {
panic(err)
}
fmt.Println(books)