How to bind data struct with database creation in Golang along with its tags/flags.
For example I have this code:
type User struct {
ID int `sql:"primary_key;AUTO_INCREMENT"`
Name string `sql:"type:varchar(100);unique"`
Email string `sql:"type:varchar(100);unique"`
Address string `sql:"type:varchar(100)"`
}
When creating the database which should be based on this struct, I instead manually create it like this:
func (db *DB) CreateUserTable() (sql.Result, error) {
statement := "CREATE TABLE IF NOT EXISTS %s (%s int, %s varchar, %s varchar, %s varchar)"
return db.Exec(
fmt.Sprintf(statement,
"user",
"id",
"name",
"email",
"address",
),
)
}
How to bind the struct and its tags(primary key, NULL, etc) in database creation?. Is there best practice for it without using ORM libraries(gorm,etc)?