I tried to written an simple ORM for MySQL, I got a problem, if a defined struct missing some field:
type User struct {
ID int64
Username string
Password string
Email string
Comment string
}
var u = User{Username: "user_0001", Password: "password"}
Some fields of User didn't given a value, then it's value will be a zeroed value, such as string ""
, bool false
, integer 0
and so on.
So I am using reflect
to get field name and value, generate a sql to insert row.
INSERT INTO User (Id, Username, Password, Email, Comment) Values (0, "user_0001", "password", , ,)
You can see there has some zeroed value of string, if I detect empty string ""
and skip them, I may skip normal value.