Database Insert records from struct


Need help in inserting the data into a table from a Struct which is similar to that of a table.
I doesn’t want to write long insert script something like this
insert into <table_name> values (?,?,?,?..)
and passing values from each item in struct.

I definitely recommend GORM :

You will basically not have to write another SQL-statement again :slight_smile: I have used it in several projects and it worked well for my (fairly small) projects.

ORM is one way to go. I do not like it, because of all the magic creates limitations and you have to learn a new “language” supported by a smaller group. Getting help with plain SQL questions is easier than getting help with an ORM question.

That said, I do not like to write code that seems to be repeated either. So I use sqlx with .StructScan. Inserts is still boring though.

And there seems to be a tool - sqlc - that creates all boring stuff automatically. Pure SQL.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.