1 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
就像是:
func LoadObject(sql string, id int, dest ...interface{}) error {
db := tools.OpenDB()
defer db.Close()
stmt, err := db.Prepare(sql)
if err != nil {
log.Panic(err)
}
defer stmt.Close()
return stmt.QueryRow(id).Scan(dest)
}
// Loads the company from the database with the given id.
func LoadCompany(id int) (c Company, err error) {
err = LoadObject("SELECT * FROM companies WHERE id = ?", &c.id,
&c.FullName, &c.Name, &c.History, &c.Overview, &c.Est, &c.Phone, &c.Website, &c.Email)
return
}
請(qǐng)注意,我還沒(méi)有編譯此代碼,但希望它足以給您一個(gè)想法。
幾點(diǎn)建議:
sql.DB
在程序啟動(dòng)時(shí)創(chuàng)建實(shí)例在 SQL 語(yǔ)句中顯式指定列順序 (
select full_name, history, .... from companies ....
)
- 1 回答
- 0 關(guān)注
- 171 瀏覽
添加回答
舉報(bào)